1 package uk.ac.sanger.artemis.components.alignment; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Component; 6 import java.awt.Font; 7 import java.awt.GridBagConstraints; 8 import java.awt.GridBagLayout; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener; 11 import java.awt.event.ItemEvent; 12 import java.awt.event.ItemListener; 13 import java.util.HashMap; 14 import java.util.Iterator; 15 16 import javax.swing.Box; 17 import javax.swing.JButton; 18 import javax.swing.JCheckBox; 19 import javax.swing.JCheckBoxMenuItem; 20 import javax.swing.JComboBox; 21 import javax.swing.JFrame; 22 import javax.swing.JLabel; 23 import javax.swing.JMenu; 24 import javax.swing.JOptionPane; 25 import javax.swing.JPanel; 26 import javax.swing.JScrollPane; 27 import javax.swing.JTextField; 28 import javax.swing.SwingUtilities; 29 30 import uk.ac.sanger.artemis.components.Utilities; 31 32 class GroupBamFrame extends JFrame 33 { 34 private static final long serialVersionUID = 1L; 35 private String[] groups = new String[]{ "Default" }; 36 private JPanel groupPanel = new JPanel(new GridBagLayout()); 37 private JPanel bamPanel = new JPanel(new GridBagLayout()); 38 private HashMap<JCheckBoxMenuItem, JComboBox> bamGroupMap = 39 new HashMap<JCheckBoxMenuItem, JComboBox> (); 40 41 private HashMap<String, JCheckBox> groupList = 42 new HashMap<String, JCheckBox>(); 43 private static final int PAD = 2; 44 45 private BamView bamView; 46 private JMenu bamFilesMenu; 47 GroupBamFrame(final BamView bamView, final JMenu bamFilesMenu)48 GroupBamFrame(final BamView bamView, final JMenu bamFilesMenu) 49 { 50 super("Alignment Groups"); 51 52 this.bamView = bamView; 53 this.bamFilesMenu = bamFilesMenu; 54 55 final JPanel centerPanel = new JPanel(new GridBagLayout()); 56 final JScrollPane jsp = new JScrollPane(centerPanel); 57 getContentPane().add(jsp, BorderLayout.CENTER); 58 centerPanel.setBackground(Color.WHITE); 59 60 /// 61 /// number of groups 62 Box xBox = Box.createHorizontalBox(); 63 xBox.add(new JLabel("Add Group:")); 64 final JTextField newGroup = new JTextField(10); 65 xBox.add(newGroup); 66 newGroup.addActionListener(new ActionListener(){ 67 public void actionPerformed(ActionEvent e) 68 { 69 addGroup(newGroup.getText().trim()); 70 } 71 }); 72 newGroup.addFocusListener(new java.awt.event.FocusAdapter() { 73 public void focusGained(java.awt.event.FocusEvent evt) { 74 SwingUtilities.invokeLater( new Runnable() { 75 public void run() { 76 newGroup.selectAll(); 77 } 78 }); 79 } 80 }); 81 82 final JButton addGroupButton = new JButton("ADD"); 83 xBox.add(addGroupButton); 84 addGroupButton.addActionListener(new ActionListener(){ 85 public void actionPerformed(ActionEvent e) 86 { 87 if(newGroup.getText() == null || 88 newGroup.getText().trim().equals("")) 89 { 90 JOptionPane.showMessageDialog(GroupBamFrame.this, 91 "Type in the name for the new group.", "Group name missing", 92 JOptionPane.INFORMATION_MESSAGE); 93 return; 94 } 95 addGroup(newGroup.getText().trim()); 96 } 97 }); 98 xBox.add(Box.createHorizontalGlue()); 99 getContentPane().add(xBox, BorderLayout.NORTH); 100 101 // 102 xBox = Box.createHorizontalBox(); 103 final JButton close = new JButton("CLOSE"); 104 close.addActionListener(new ActionListener(){ 105 public void actionPerformed(ActionEvent arg0) 106 { 107 setVisible(false); 108 } 109 }); 110 xBox.add(close); 111 xBox.add(Box.createHorizontalGlue()); 112 getContentPane().add(xBox, BorderLayout.SOUTH); 113 114 115 /// 116 /// 117 final GridBagConstraints c = new GridBagConstraints(); 118 c.gridy = 0; 119 c.gridx = 0; 120 c.ipadx = PAD; 121 c.ipady = PAD; 122 c.anchor = GridBagConstraints.WEST; 123 c.gridwidth = 2; 124 final JLabel alignLabel = new JLabel("ALIGNMENTS:"); 125 alignLabel.setFont(alignLabel.getFont().deriveFont(Font.BOLD)); 126 centerPanel.add(alignLabel, c); 127 c.gridwidth = 1; 128 c.gridy += 1; 129 centerPanel.add(Box.createVerticalStrut(5), c); 130 c.gridy += 1; 131 132 bamPanel.setBackground(Color.WHITE); 133 centerPanel.add(bamPanel, c); 134 135 c.gridx = 10; 136 c.gridheight = GridBagConstraints.REMAINDER; 137 centerPanel.add(groupPanel, c); 138 groupPanel.setBackground(Color.WHITE); 139 140 Utilities.rightJustifyFrame(this); 141 } 142 updateAndDisplay()143 protected void updateAndDisplay() 144 { 145 updateBamPanel(); 146 updateGroupPanel(); 147 pack(); 148 setVisible(true); 149 } 150 addGroup(final String newGroup)151 protected void addGroup(final String newGroup) 152 { 153 final String tmpGroups[] = new String[groups.length+1]; 154 System.arraycopy(groups, 0, tmpGroups, 0, groups.length); 155 tmpGroups[tmpGroups.length-1] = newGroup; 156 groups = tmpGroups; 157 158 updateGroupPanel(); 159 updateBamPanel(); 160 validate(); 161 } 162 updateBamPanel()163 private void updateBamPanel() 164 { 165 final Component cs[] = bamFilesMenu.getMenuComponents(); 166 bamPanel.removeAll(); 167 final GridBagConstraints c = new GridBagConstraints(); 168 c.gridy = 0; 169 c.gridx = 0; 170 c.ipadx = PAD; 171 c.ipady = PAD; 172 c.anchor = GridBagConstraints.WEST; 173 174 for(Component cp : cs) 175 { 176 if(cp instanceof JCheckBoxMenuItem) 177 { 178 final JCheckBoxMenuItem cbBam = (JCheckBoxMenuItem) cp; 179 final String bam = cbBam.getText(); 180 c.gridy += 1; 181 c.gridx = 0; 182 bamPanel.add(new JLabel(bam), c); 183 184 c.gridx = 1; 185 bamPanel.add(new JLabel(cbBam.getIcon()), c); 186 187 c.gridx = 2; 188 JComboBox groupCombo = new JComboBox( groups ); 189 bamPanel.add(groupCombo, c); 190 191 if(bamGroupMap.containsKey(cbBam)) 192 groupCombo.setSelectedItem( 193 (String) bamGroupMap.get(cbBam).getSelectedItem()); 194 195 bamGroupMap.put(cbBam, groupCombo); 196 } 197 } 198 } 199 200 /** 201 * Add a BAM to a specified group 202 * @param bam 203 * @param group 204 */ addToGroup(String bam, String group)205 protected void addToGroup(String bam, String group) 206 { 207 final Component cs[] = bamFilesMenu.getMenuComponents(); 208 for(Component cp : cs) 209 { 210 if(cp instanceof JCheckBoxMenuItem) 211 { 212 final JCheckBoxMenuItem cbBam = (JCheckBoxMenuItem) cp; 213 final String thisBam = cbBam.getText(); 214 if(bam.equals(thisBam)) 215 { 216 bamGroupMap.get(cbBam).setSelectedItem(group); 217 return; 218 } 219 } 220 } 221 } 222 223 /** 224 * For a give BAM file return the group it belongs to. 225 * @param bamName 226 * @return 227 */ getGroupName(final String bamName)228 protected String getGroupName(final String bamName) 229 { 230 Iterator<JCheckBoxMenuItem> it = bamGroupMap.keySet().iterator(); 231 while(it.hasNext()) 232 { 233 JCheckBoxMenuItem cbs = it.next(); 234 if(cbs.getText().equals(bamName)) 235 return (String) bamGroupMap.get(cbs).getSelectedItem(); 236 } 237 return null; 238 } 239 getNumberOfGroups()240 protected int getNumberOfGroups() 241 { 242 return groups.length; 243 } 244 245 /** 246 * Find the maximum number of BAM files found in the groups. 247 * @return 248 */ getMaximumBamsInGroup()249 protected int getMaximumBamsInGroup() 250 { 251 int max = 1; 252 int grpMax[] = new int[getNumberOfGroups()]; 253 for(int i=0; i<grpMax.length; i++) 254 grpMax[i] = 0; 255 Iterator<JCheckBoxMenuItem> it = bamGroupMap.keySet().iterator(); 256 while(it.hasNext()) 257 { 258 JCheckBoxMenuItem cbs = it.next(); 259 String grp = (String) bamGroupMap.get(cbs).getSelectedItem(); 260 for(int i=0; i<groups.length; i++) 261 { 262 if(grp.equals(groups[i])) 263 { 264 grpMax[i]++; 265 break; 266 } 267 } 268 } 269 270 for(int i=0; i<grpMax.length; i++) 271 { 272 if(grpMax[i] > max) 273 max = grpMax[i]; 274 } 275 return max; 276 } 277 updateGroupPanel()278 private void updateGroupPanel() 279 { 280 groupPanel.removeAll(); 281 final GridBagConstraints c = new GridBagConstraints(); 282 c.gridy = 0; 283 c.gridx = 0; 284 c.anchor = GridBagConstraints.WEST; 285 286 final JLabel groupLabel = new JLabel("GROUPS:"); 287 groupLabel.setFont(groupLabel.getFont().deriveFont(Font.BOLD)); 288 groupPanel.add(groupLabel, c); 289 c.gridy += 2; 290 for(final String group: groups) 291 { 292 c.gridy += 1; 293 294 final JCheckBox cbox; 295 if(groupList.containsKey(group)) 296 cbox = groupList.get(group); 297 else 298 { 299 cbox = new JCheckBox(group, true); 300 groupList.put(group, cbox); 301 } 302 groupPanel.add(cbox, c); 303 cbox.addItemListener(new ItemListener(){ 304 public void itemStateChanged(ItemEvent arg0) 305 { 306 Iterator<JCheckBoxMenuItem> it = bamGroupMap.keySet().iterator(); 307 while(it.hasNext()) 308 { 309 JCheckBoxMenuItem cbs = it.next(); 310 String thisGroup = (String) bamGroupMap.get(cbs).getSelectedItem(); 311 if(group.equals(thisGroup)) 312 cbs.setSelected(cbox.isSelected()); 313 } 314 } 315 }); 316 } 317 } 318 319 320 }