/** * Dumps the layout's column specifications to the console. * * @param layout the <code>FormLayout</code> to inspect */ public static void dumpColumnSpecs(FormLayout layout) { System.out.print("COLUMN SPECS:"); for (int col = 1; col <= layout.getColumnCount(); col++) { ColumnSpec colSpec = layout.getColumnSpec(col); System.out.print(colSpec.toShortString()); if (col < layout.getColumnCount()) System.out.print(", "); } System.out.println(); }
private void prepareComponent() { CloseEventHandler closeEventHandler = new CloseEventHandler(); addWindowListener(closeEventHandler); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel contentPanel = new JPanel(); // JPanel contentPanel = new FormDebugPanel(); contentPane.add(contentPanel, BorderLayout.CENTER); CellConstraints cc = new CellConstraints(); FormLayout layout = new FormLayout( "4dlu, d:grow, 4dlu", // columns "4dlu, p, 2dlu, fill:100dlu:grow, 4dlu, " + // rows "p, 2dlu, p, 4dlu"); // btn rows PanelBuilder contentPB = new PanelBuilder(layout, contentPanel); int columnCount = layout.getColumnCount(); int rowCount = layout.getRowCount(); JLabel label = new JLabel(Localizer.getString("MediaTypeCondEditor_ConditionToAdd")); contentPB.add(label, cc.xywh(2, 2, 1, 1)); mediaTypeModel = new MediaTypeModel(); mediaTypeTable = new JTable(mediaTypeModel); JTableHeader tableHeader = mediaTypeTable.getTableHeader(); tableHeader.setResizingAllowed(false); tableHeader.setReorderingAllowed(false); // adjust column witdh of checkbox JCheckBox box = (JCheckBox) mediaTypeTable.getDefaultRenderer(Boolean.class); TableColumn column = mediaTypeTable.getColumnModel().getColumn(0); column.setMaxWidth(box.getPreferredSize().width + 2); column.setMinWidth(box.getPreferredSize().width + 2); mediaTypeTable.getColumnModel().getColumn(1).setCellRenderer(new MediaTypeCellRenderer()); // ToolTipManager.sharedInstance().registerComponent( mediaTypeTable ); contentPB.add(new JScrollPane(mediaTypeTable), cc.xywh(2, 4, 1, 1)); // button bar contentPB.add(new JSeparator(), cc.xywh(1, rowCount - 3, columnCount, 1)); JButton okBtn = new JButton(Localizer.getString("OK")); okBtn.addActionListener(new OkBtnListener()); okBtn.setDefaultCapable(true); okBtn.setRequestFocusEnabled(true); JButton cancelBtn = new JButton(Localizer.getString("Cancel")); cancelBtn.addActionListener(closeEventHandler); JPanel btnPanel = ButtonBarFactory.buildOKCancelBar(okBtn, cancelBtn); contentPB.add(btnPanel, cc.xywh(2, rowCount - 1, columnCount - 2, 1)); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); getRootPane().setDefaultButton(okBtn); pack(); setLocationRelativeTo(getParent()); }
/** Add a new row to the current tab. */ private void addColumn() { JPanel selected = (JPanel) tabPane.getSelectedComponent(); FormLayout layout = (FormLayout) selected.getLayout(); layout.appendColumn(new ColumnSpec("pref:grow")); int ndx = tabPane.getSelectedIndex(); for (int i = 0; i < layout.getRowCount(); i++) { addIcon(selected, i, layout.getColumnCount() - 1, ndx); } tabPane.repaint(); }
public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("new")) { icons = new Hashtable[0]; fields = new Hashtable[0]; tabPane.removeAll(); repaint(); } else if (cmd.equals("open")) { JFileChooser chooser = new JFileChooser(); FileFilter filter = new FileFilter() { public boolean accept(File f) { return f.getAbsolutePath().endsWith(".template") || f.isDirectory(); } public String getDescription() { return "OME Notes Templates"; } }; chooser.setFileFilter(filter); int status = chooser.showOpenDialog(this); if (status == JFileChooser.APPROVE_OPTION) { String file = chooser.getSelectedFile().getAbsolutePath(); try { Template t = new Template(file); TemplateTab[] tabs = t.getTabs(); for (int i = 0; i < tabs.length; i++) { int rows = tabs[i].getRows(); int cols = tabs[i].getColumns(); if (cols == 0) cols = 1; if (rows == 0) rows = 1; addTab(tabs[i].getName(), rows, cols); tabPane.setSelectedIndex(i); for (int j = 0; j < tabs[i].getNumFields(); j++) { TemplateField f = tabs[i].getField(j); int x = f.getRow(); int y = f.getColumn(); if (x == -1) x = 1; if (y == -1) y = j + 1; Point p = new Point(x, y); DraggableIcon icon = (DraggableIcon) icons[i].get(p); icon.label = new JLabel(f.getName()); JPanel panel = new JPanel(); panel.add(f.getComponent()); icon.setPanel(panel); } } } catch (Exception exc) { error("Failed to parse template", exc); } tabPane.setSelectedIndex(0); } } else if (cmd.equals("save")) { // build up the template from the components TemplateTab[] tabs = new TemplateTab[tabPane.getTabCount()]; for (int i = 0; i < tabs.length; i++) { tabs[i] = new TemplateTab(); tabs[i].setName(tabPane.getTitleAt(i)); JComponent c = (JComponent) tabPane.getComponentAt(i); FormLayout layout = (FormLayout) c.getLayout(); tabs[i].setRows(layout.getRowCount()); tabs[i].setColumns(layout.getColumnCount()); Object[] keys = icons[i].keySet().toArray(); for (int j = 0; j < keys.length; j++) { Point p = (Point) keys[j]; DraggableIcon icon = (DraggableIcon) icons[i].get(p); TemplateField f = (TemplateField) fields[i].get(p); if (icon.image != null) { Component[] components = icon.image.getComponents(); JLabel label = icon.label; JComponent component = (JComponent) components[0]; f.setComponent(component); for (int k = 0; k < COMPONENTS.length; k++) { if (component.getClass().equals(COMPONENTS[k])) { f.setType(COMPONENT_TYPES[k]); break; } } f.setRow(p.y); f.setColumn(p.x); f.setDefaultValue(TemplateTools.getComponentValue(component)); tabs[i].addField(f); } } } Template t = new Template(tabs, null); // prompt for filename to save to if (currentFile == null) { JFileChooser chooser = new JFileChooser(); FileFilter filter = new FileFilter() { public boolean accept(File f) { return true; } public String getDescription() { return "All files"; } }; chooser.setFileFilter(filter); int status = chooser.showSaveDialog(this); if (status == JFileChooser.APPROVE_OPTION) { currentFile = chooser.getSelectedFile().getAbsolutePath(); if (currentFile == null) return; } } try { t.save(currentFile); } catch (IOException io) { error("Failed to save template", io); } } else if (cmd.equals("quit")) dispose(); else if (cmd.equals("add row")) addRow(); else if (cmd.equals("add col")) addColumn(); else if (cmd.equals("prompt tab")) { // prompt for tab name JPopupMenu menu = new JPopupMenu(); newTabName = new JTextField(); newTabName.setPreferredSize(new Dimension(200, 25)); menu.add(newTabName); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("new tab"); menu.add(b); JComponent s = (JComponent) e.getSource(); menu.show(s, s.getX(), s.getY()); newTabName.grabFocus(); } else if (cmd.equals("new tab")) { newTabName.getParent().setVisible(false); addTab(newTabName.getText(), 2, 2); } else if (cmd.equals("setName")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); String text = ((JTextField) menu.getComponents()[0]).getText(); Point p = new Point(icon.gridx, icon.gridy); int ndx = tabPane.getSelectedIndex(); TemplateField f = (TemplateField) fields[ndx].get(p); f.setName(text); f.setNameMap(null); // set the name if (icon.label != null) icon.remove(icon.label); icon.remove(icon.image); icon.label = new JLabel(text); icon.add(icon.label); icon.add(icon.image); icon.getParent().repaint(); } else if (cmd.equals("changeName")) { // prompt for new field name JPopupMenu menu = new JPopupMenu(); JTextField field = new JTextField(); field.setPreferredSize(new Dimension(200, 25)); menu.add(field); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("setName"); menu.add(b); menu.show(lastMenuComponent, lastMenuX, lastMenuY); field.grabFocus(); } else if (cmd.equals("nameMap")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); MappingWindow w = new MappingWindow(this, true); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("map")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); MappingWindow w = new MappingWindow(this, false); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("repeat")) { JMenuItem item = (JMenuItem) e.getSource(); DraggableIcon icon = (DraggableIcon) ((JPopupMenu) item.getParent()).getInvoker(); TemplateField f = getField(icon); if (item.getText().equals("Repeat this field")) { item.setText("Don't repeat this field"); f.setRepeated(true); } else { item.setText("Repeat this field"); f.setRepeated(false); } } else if (cmd.equals("removeField")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); int idx = tabPane.getSelectedIndex(); Object[] keys = icons[idx].keySet().toArray(); for (int i = 0; i < keys.length; i++) { if (icons[idx].get(keys[i]).equals(icon)) { icons[idx].remove(keys[i]); fields[idx].remove(keys[i]); break; } } icon.remove(icon.label); icon.remove(icon.image); tabPane.repaint(); } else if (cmd.startsWith("removeRow")) { int row = Integer.parseInt(cmd.substring(cmd.indexOf(":") + 1)); JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); menu.setVisible(false); JPanel pane = (JPanel) tabPane.getSelectedComponent(); FormLayout layout = (FormLayout) pane.getLayout(); int rows = layout.getRowCount(); int cols = layout.getColumnCount(); int idx = tabPane.getSelectedIndex(); for (int i = 0; i < cols; i++) { pane.remove((JComponent) icons[idx].get(new Point(i + 1, row + 1))); } rekey(row, -1); layout.removeRow(row + 1); tabPane.repaint(); } else if (cmd.startsWith("removeColumn")) { int col = Integer.parseInt(cmd.substring(cmd.indexOf(":") + 1)); JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); menu.setVisible(false); JPanel pane = (JPanel) tabPane.getSelectedComponent(); FormLayout layout = (FormLayout) pane.getLayout(); int rows = layout.getRowCount(); int cols = layout.getColumnCount(); int idx = tabPane.getSelectedIndex(); for (int i = 0; i < rows; i++) { pane.remove((JComponent) icons[idx].get(new Point(col + 1, i + 1))); } rekey(-1, col); layout.removeColumn(col + 1); tabPane.repaint(); } else if (cmd.equals("removeTab")) { int ndx = tabPane.getSelectedIndex(); tabPane.remove(ndx); Hashtable[] h = new Hashtable[icons.length - 1]; Hashtable[] f = new Hashtable[fields.length - 1]; System.arraycopy(icons, 0, h, 0, ndx); System.arraycopy(icons, ndx + 1, h, ndx, h.length - ndx); System.arraycopy(fields, 0, f, 0, ndx); System.arraycopy(fields, ndx + 1, f, ndx, f.length - ndx); icons = h; fields = f; } else if (cmd.equals("specifyChoices")) { JMenuItem item = (JMenuItem) e.getSource(); DraggableIcon icon = (DraggableIcon) ((JPopupMenu) item.getParent()).getInvoker(); TemplateField f = getField(icon); EnumWindow w = new EnumWindow(this, f.getEnums()); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("setThumbSource")) { JPopupMenu menu = new JPopupMenu(); ButtonGroup g = new ButtonGroup(); JRadioButton dataset = new JRadioButton("Use thumbnail from dataset"); dataset.setSelected(true); g.add(dataset); JRadioButton file = new JRadioButton("Use thumbnail from file:"); g.add(file); menu.add(dataset); menu.add(file); JTextField field = new JTextField(); field.setPreferredSize(new Dimension(200, 25)); menu.add(field); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("applyThumbSource"); menu.add(b); menu.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("applyThumbSource")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); Component[] c = menu.getComponents(); JRadioButton dataset = (JRadioButton) c[0]; String text = null; if (!dataset.isSelected()) { JTextField t = (JTextField) c[2]; text = t.getText(); getField(icon).setValueMap(text); } menu.setVisible(false); if (text != null) { try { BufferedImageReader reader = new BufferedImageReader(); reader.setId(text); BufferedImage thumb = reader.openThumbImage(0); JLabel label = (JLabel) icon.image.getComponents()[0]; label.setIcon(new ImageIcon(thumb)); reader.close(); } catch (FormatException exc) { error("Failed to open thumbnail (" + text + ")", exc); } catch (IOException exc) { error("Failed to open thumbnail (" + text + ")", exc); } } } else if (cmd.equals("ok")) { // this event came from an instance of EnumWindow JPanel parent = (JPanel) ((JButton) e.getSource()).getParent(); EnumWindow menu = (EnumWindow) parent.getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); TemplateField f = getField(icon); menu.setVisible(false); String[] options = menu.getOptions(); f.setEnums(options); JComboBox box = (JComboBox) icon.image.getComponents()[0]; for (int i = 0; i < options.length; i++) box.addItem(options[i]); repaint(); } else if (cmd.equals("chooseMapping")) { // this event came from an instance of MappingWindow JTabbedPane parent = (JTabbedPane) ((JButton) e.getSource()).getParent(); MappingWindow menu = (MappingWindow) parent.getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); TemplateField f = getField(icon); String omexmlMap = null; if (menu.nameMap) f.setNameMap(omexmlMap); else f.setValueMap(omexmlMap); menu.setVisible(false); } }