private ToolAdapterOperatorDescriptor requestSelection() { ToolAdapterOperatorDescriptor selected = null; int selectedRow = operatorsTable.getSelectedRow(); if (selectedRow >= 0) { selected = ((OperatorsTableModel) operatorsTable.getModel()).getObjectAt(selectedRow); } else { SnapDialogs.showWarning(Bundle.MessageNoSelection_Text()); } return selected; }
private JTable createAdaptersPanel() { java.util.List<ToolAdapterOperatorDescriptor> toolboxSpis = new ArrayList<>(); toolboxSpis.addAll( ToolAdapterRegistry.INSTANCE .getOperatorMap() .values() .stream() .map(e -> (ToolAdapterOperatorDescriptor) e.getOperatorDescriptor()) .collect(Collectors.toList())); toolboxSpis.sort((o1, o2) -> o1.getAlias().compareTo(o2.getAlias())); OperatorsTableModel model = new OperatorsTableModel(toolboxSpis); operatorsTable = new JTable(model); operatorsTable.getColumnModel().getColumn(0).setMaxWidth(250); operatorsTable.getColumnModel().getColumn(1).setMaxWidth(LABEL_COLUMN_WIDTH); operatorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); operatorsTable.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { int selectedRow = operatorsTable.getSelectedRow(); // operatorsTable.getModel().setValueAt(true, selectedRow, 0); operatorsTable.repaint(); ToolAdapterOperatorDescriptor operatorDesc = ((OperatorsTableModel) operatorsTable.getModel()).getObjectAt(selectedRow); ToolAdapterEditorDialog dialog = new ToolAdapterEditorDialog(appContext, operatorDesc, false); dialog.show(); refreshContent(); } } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} }); return operatorsTable; }
private JPanel createContentPanel() { // compute content and other buttons SpringLayout springLayout = new SpringLayout(); JPanel panel = new JPanel(springLayout); int panelHeight = 0; JTable propertiesPanel = createPropertiesPanel(); panelHeight += propertiesPanel.getPreferredSize().getHeight(); panel.add(propertiesPanel); panelHeight += 10; panel.add(Box.createVerticalStrut(10)); JScrollPane scrollPane = new JScrollPane(createAdaptersPanel()); panelHeight += scrollPane.getPreferredSize().getHeight(); panel.add(scrollPane); panelHeight += 10; panel.add(Box.createVerticalStrut(10)); JPanel buttonsPanel = createButtonsPanel(); panelHeight += buttonsPanel.getPreferredSize().getHeight(); panel.add(buttonsPanel); springLayout.putConstraint( SpringLayout.NORTH, panel, DEFAULT_PADDING, SpringLayout.NORTH, propertiesPanel); springLayout.putConstraint( SpringLayout.WEST, panel, DEFAULT_PADDING, SpringLayout.WEST, propertiesPanel); springLayout.putConstraint( SpringLayout.EAST, panel, DEFAULT_PADDING, SpringLayout.EAST, propertiesPanel); springLayout.putConstraint( SpringLayout.NORTH, scrollPane, DEFAULT_PADDING, SpringLayout.SOUTH, propertiesPanel); springLayout.putConstraint( SpringLayout.WEST, panel, DEFAULT_PADDING, SpringLayout.WEST, scrollPane); springLayout.putConstraint( SpringLayout.EAST, panel, DEFAULT_PADDING, SpringLayout.EAST, scrollPane); springLayout.putConstraint( SpringLayout.NORTH, scrollPane, DEFAULT_PADDING, SpringLayout.SOUTH, buttonsPanel); springLayout.putConstraint( SpringLayout.WEST, panel, DEFAULT_PADDING, SpringLayout.WEST, buttonsPanel); springLayout.putConstraint( SpringLayout.EAST, panel, DEFAULT_PADDING, SpringLayout.EAST, buttonsPanel); panel.setPreferredSize( new Dimension( CHECK_COLUMN_WIDTH + LABEL_COLUMN_WIDTH + COLUMN_WIDTH - 32, panelHeight + DEFAULT_PADDING)); makeCompactGrid(panel, 5, 1, 0, 0, 0, 0); return panel; }
private JTable createPropertiesPanel() { DefaultTableModel model = new DefaultTableModel(1, 2) { @Override public boolean isCellEditable(int row, int column) { return column == 1; } }; model.setValueAt(Bundle.PathLabel_Text(), 0, 0); model.setValueAt(ToolAdapterIO.getUserAdapterPath(), 0, 1); model.addTableModelListener( l -> { String newPath = model.getValueAt(0, 1).toString(); File path = new File(newPath); if (!path.exists() && SnapDialogs.Answer.YES == SnapDialogs.requestDecision( "Path does not exist", "The path you have entered does not exist.\nDo you want to create it?", true, "Don't ask me in the future")) { if (!path.mkdirs()) { SnapDialogs.showError("Path could not be created!"); } } if (path.exists()) { File oldPath = ToolAdapterIO.getUserAdapterPath(); ToolAdapterOperatorDescriptor[] operatorDescriptors = ToolAdapterActionRegistrar.getActionMap() .values() .toArray( new ToolAdapterOperatorDescriptor [ToolAdapterActionRegistrar.getActionMap().values().size()]); for (ToolAdapterOperatorDescriptor descriptor : operatorDescriptors) { ToolAdapterActionRegistrar.removeOperatorMenu(descriptor); } ToolAdapterIO.setAdaptersPath(Paths.get(newPath)); if (!newPath.equals(oldPath.getAbsolutePath())) { Collection<ToolAdapterOpSpi> toolAdapterOpSpis = ToolAdapterIO.searchAndRegisterAdapters(); for (ToolAdapterOpSpi spi : toolAdapterOpSpis) { ToolAdapterActionRegistrar.registerOperatorMenu( (ToolAdapterOperatorDescriptor) spi.getOperatorDescriptor()); } refreshContent(); } } }); JTable table = new JTable(model); TableColumn labelColumn = table.getColumnModel().getColumn(0); labelColumn.setPreferredWidth((CHECK_COLUMN_WIDTH + LABEL_COLUMN_WIDTH) / 2); TableColumn pathColumn = table.getColumnModel().getColumn(1); pathColumn.setPreferredWidth(COLUMN_WIDTH); pathColumn.setCellEditor(new FileChooserCellEditor()); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); table.setRowHeight(PATH_ROW_HEIGHT); table.setBorder(BorderFactory.createLineBorder(Color.black)); table.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) {} @Override public void focusLost(FocusEvent e) { Object source = e.getSource(); if (!table.equals(source)) { table.editingCanceled(new ChangeEvent(source)); table.clearSelection(); } } }); return table; }