List of usage examples for javax.swing.table TableColumn setMaxWidth
@BeanProperty(description = "The maximum width of the column.") public void setMaxWidth(int maxWidth)
TableColumn
's maximum width to maxWidth
or, if maxWidth
is less than the minimum width, to the minimum width. From source file:net.sf.profiler4j.console.ProjectDialog.java
/** * This method initializes rulesTable/*w w w. j ava 2s. com*/ * * @return javax.swing.JTable */ private JTable getRulesTable() { if (rulesTable == null) { rulesTable = new JTable(); rulesTable.setModel(ruleTableModel); rulesTable.setRowMargin(4); rulesTable.setRowHeight(24); rulesTable.setFont(new Font("Monospaced", Font.PLAIN, 14)); TableColumn c; c = rulesTable.getColumnModel().getColumn(0); c.setMinWidth(300); c = rulesTable.getColumnModel().getColumn(1); c.setMinWidth(80); c.setMaxWidth(80); JComboBox editorCb = new JComboBox(); for (Rule.Action a : Rule.Action.values()) { editorCb.addItem(a); } c.setCellEditor(new DefaultCellEditor(editorCb)); c.setCellRenderer(new DefaultTableCellRenderer() { Font font = new Font("Monospaced", Font.BOLD, 13); @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); Rule.Action r = (Rule.Action) value; setHorizontalAlignment(CENTER); setFont(font); if (r == Rule.Action.ACCEPT) { setBackground(Color.GREEN); } else { setBackground(Color.RED); } if (isSelected) { setForeground(Color.YELLOW); } else { setForeground(Color.BLACK); } return this; } }); } return rulesTable; }
From source file:org.jdal.swing.ListTableModel.java
/** * Create a TableColumnModel for JTable. * Try to use sizes and cell renderers from property descriptors. * @return a new TableColumnModel based on PropertyDescriptors *//*from w w w . j a v a 2 s .co m*/ public TableColumnModel getTableColumnModel() { TableColumnModel tcm = new DefaultTableColumnModel(); int baseIndex = 0; if (usingChecks) { TableColumn tableColumn = new TableColumn(0); tableColumn.setMaxWidth(50); tcm.addColumn(tableColumn); baseIndex++; } for (int i = 0; i < columnNames.size(); i++) { String name = this.columnNames.get(i); TableColumn tableColumn = new TableColumn(baseIndex + i); tableColumn.setHeaderValue(displayNames.get(i)); if (pds != null && pds.size() > 0) { PropertyDescriptor descriptor = pds.get(i); // property values for TableColumns if (descriptor != null) { Integer maxWidth = getColumnWidth(name); if (maxWidth != null) tableColumn.setMaxWidth(maxWidth.intValue()); tableColumn.setCellRenderer(getColumnRenderer(name)); tableColumn.setCellEditor(getColumnEditor(name)); } } tcm.addColumn(tableColumn); } if (usingActions) { baseIndex += columnNames.size(); for (int i = 0; i < actions.size(); i++) { TableColumn tableColumn = new TableColumn(baseIndex + i); tableColumn.setCellRenderer(new ActionCellRenderer()); tableColumn.setMaxWidth(50); // tableColumn.setCellEditor(new ActionCellEditor()) tcm.addColumn(tableColumn); } } return tcm; }
From source file:com.github.fritaly.dualcommander.DirectoryBrowser.java
public DirectoryBrowser(UserPreferences preferences, File directory) { Validate.notNull(preferences, "The given user preferences are null"); Validate.notNull(directory, "The given directory is null"); Validate.isTrue(directory.exists(),//from w ww . ja v a 2 s .com String.format("The given directory '%s' doesn't exist", directory.getAbsolutePath())); Validate.isTrue(directory.isDirectory(), String.format("The given path '%s' doesn't denote a directory", directory.getAbsolutePath())); this.preferences = preferences; // Layout, columns & rows setLayout(new MigLayout("insets 0px", "[grow]", "[]1[grow]1[]")); this.tableModel = new FileTableModel(this); this.table = new JTable(tableModel); this.table.setBackground(Utils.getDefaultBackgroundColor()); this.table.addKeyListener(this); this.table.addMouseListener(this); this.table.addFocusListener(this); this.table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); this.table.getSelectionModel().addListSelectionListener(this); // Listen to column event resize events final ColumnEventHelper eventHelper = new ColumnEventHelper(this); this.table.getColumnModel().addColumnModelListener(eventHelper); this.table.getTableHeader().addMouseListener(eventHelper); // Render the table headers with a bold font this.table.getTableHeader().setFont(Utils.getBoldFont(this.table.getTableHeader().getFont())); this.table.getTableHeader().setBackground(Utils.getDefaultBackgroundColor()); this.table.getTableHeader().setDefaultRenderer(new TableHeaderRenderer()); this.table.getTableHeader().addMouseListener(this); final TableColumn typeColumn = this.table.getColumn(FileTableModel.COLUMN_TYPE); typeColumn.setCellRenderer(new FileTypeRenderer()); typeColumn.setResizable(false); typeColumn.setHeaderValue(""); typeColumn.setMaxWidth(Icons.FOLDER_ICON.getIconWidth() + 5); final TableColumn fileColumn = this.table.getColumn(FileTableModel.COLUMN_NAME); fileColumn.setCellRenderer(new FileNameRenderer()); fileColumn.setResizable(true); final TableColumn sizeColumn = this.table.getColumn(FileTableModel.COLUMN_SIZE); sizeColumn.setCellRenderer(new FileSizeRenderer()); sizeColumn.setResizable(true); // Dynamically set the column to the correct size final TableColumn lastUpdateColumn = this.table.getColumn(FileTableModel.COLUMN_LAST_UPDATE); lastUpdateColumn.setCellRenderer(new LastUpdateRenderer()); lastUpdateColumn.setResizable(false); lastUpdateColumn.setMaxWidth(Utils.getTimestampRenderWidth() + 5); lastUpdateColumn.setMinWidth(Utils.getTimestampRenderWidth() + 5); // Use a square border (not one with rounded corners) this.directoryButton.setBorder(Utils.createRaisedBevelBorder()); this.directoryButton.setFont(Utils.getDefaultFont()); this.directoryButton.setFocusable(false); this.directoryButton.setHorizontalAlignment(SwingConstants.LEFT); this.summary = new JLabel(" "); this.summary.setBorder(Utils.createRaisedBevelBorder()); add(directoryButton, "grow, wrap"); add(new JScrollPane(table), "grow, wrap"); add(summary, "grow"); // Set the directory (this will populate the table) setDirectory(directory); }
From source file:es.emergya.ui.plugins.AdminPanel.java
/** * Cambia los datos que muestra la tabla al array que se le pase. * /* ww w . j a v a 2 s . c om*/ * Se aconseja que sean: * {@link Boolean} para valores si/no * * {@link AbstractAction} o subclases para botones * Numeros * * {@link String} para todo lo demas * * @param data */ public void setTableData(final Object[][] data) { final Object[][] newData = new Object[data.length][]; SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() { private MyTableModel model; @Override protected Object doInBackground() throws Exception { if (data == null) { return null; } model = (MyTableModel) table.getModel(); synchronized (seleccion) { for (int i = 0; i < model.getRowCount(); i++) { if ((Boolean) model.getValueAt(i, 0)) { seleccion.add(model.getValueAt(i, columnToReselect)); } } } synchronized (seleccion) { for (int i = 0; i < data.length; i++) { newData[i] = new Object[data[0].length + 1]; newData[i][0] = new Boolean(Authentication.isAuthenticated() && seleccion.contains(data[i][columnToReselect - 1])); for (int j = 0; j < data[i].length; j++) { newData[i][j + 1] = data[i][j]; } } } return null; } protected void done() { model.updateRows(newData); if (!initialized) { table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); for (Integer i : colsWidth.keySet()) { try { final TableColumn column = table.getColumnModel().getColumn(i); final TableColumn filtro = filters.getColumnModel().getColumn(i); column.setPreferredWidth(colsWidth.get(i)); column.setMinWidth(colsWidth.get(i)); column.setMaxWidth(colsWidth.get(i)); filtro.setPreferredWidth(colsWidth.get(i)); filtro.setMinWidth(colsWidth.get(i)); filtro.setMaxWidth(colsWidth.get(i)); } catch (Throwable t) { log.error("Error al resizar las columnas: " + t); } } TableColumn col = table.getColumnModel().getColumn(0); TableColumn fil = filters.getColumnModel().getColumn(0); log.trace("Resizando CheckBox"); col.setMaxWidth(49); fil.setMaxWidth(49); int defaultWidth = 54; for (int i = 1; i < table.getColumnModel().getColumnCount() - 2; i++) { col = table.getColumnModel().getColumn(i); fil = filters.getColumnModel().getColumn(i); final Class<?> columnClass = ((MyTableModel) table.getModel()).getColumnClass(i); if (columnClass == JButton.class) { log.trace("Resizando JButton"); col.setMaxWidth(defaultWidth); fil.setMaxWidth(defaultWidth); } else if (columnClass == Boolean.class) { log.trace("Resizando CheckBox"); col.setMaxWidth(49); fil.setMaxWidth(49); } } if (getCanDelete()) { col = table.getColumnModel().getColumn(table.getColumnModel().getColumnCount() - 2); col.setMaxWidth(defaultWidth); col.setPreferredWidth(defaultWidth); col = table.getColumnModel().getColumn(table.getColumnModel().getColumnCount() - 1); col.setMaxWidth(defaultWidth); col.setPreferredWidth(defaultWidth); } else { col = table.getColumnModel().getColumn(table.getColumnModel().getColumnCount() - 1); col.setMaxWidth(defaultWidth * 2); col.setPreferredWidth(defaultWidth * 2); } int max = filters.getColumnModel().getColumnCount() - 1; filters.getColumnModel().getColumn(max).setMaxWidth(61); filters.getColumnModel().getColumn(max - 1).setMaxWidth(32); filters.getColumnModel().getColumn(max - 2).setMaxWidth(32); initialized = true; } } }; sw.execute(); }
From source file:gov.noaa.ncdc.iosp.avhrr.util.AvhrrLevel1B2Netcdf.java
private void setTableCellRenderer(JTable table, TableCellRenderer renderer) { TableColumnModel columnModel = table.getColumnModel(); int columnCount = columnModel.getColumnCount(); for (int i = 0; i < columnCount; i++) { TableColumn column = columnModel.getColumn(i); if (i == 0) { column.setMaxWidth(90); } else {/*from ww w. j a va 2 s. c o m*/ if (i > 1) { column.setMaxWidth(90); } if (i == (columnCount - 1)) { column.setMaxWidth(0); } if (i == (columnCount - 2)) { column.setMaxWidth(150); } column.setCellRenderer(renderer); } } }
From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java
/** * Creates notebook outline.// w w w . j av a2s. c o m */ private OutlineJPanel() { setDoubleBuffered(true); setLayout(new BorderLayout()); /* * toolbar */ JToolBar toolbar = createToolbar(); /* * tree */ // tree table itself outlineTableTree = OutlineTreeInstance.getInstance(); outlineTableTreeModel = new NotebookOutlineModel(outlineTableTree.getOutlineRoot()); treeTable = new JTreeTable(outlineTableTreeModel); treeTable.tree.addTreeSelectionListener(new TreeSelectionListenerImplementation()); // add key listener treeTable.addKeyListener(new KeyListenerImplementation()); // label column TableColumn tableColumn = treeTable .getColumn(NotebookOutlineModel.columnNames[NotebookOutlineModel.COLUMN_LABEL]); tableColumn.setMaxWidth(LABEL_COLUMN_MAX_WIDTH); tableColumn.setMinWidth(0); tableColumn.setPreferredWidth(LABEL_COLUMN_PREFERRED_WIDTH); // date column tableColumn = treeTable.getColumn(NotebookOutlineModel.columnNames[NotebookOutlineModel.COLUMN_CREATED]); tableColumn.setMaxWidth(DATE_COLUMN_MAX_WIDTH); tableColumn.setMinWidth(0); tableColumn.setPreferredWidth(DATE_COLUMN_PREFERRED_WIDTH); // and the rest will be annotation JScrollPane treeTableScrollPane = new JScrollPane(treeTable); // outline treetabble + toolbar panel JPanel treeAndToolbarPanel = new JPanel(new BorderLayout()); treeAndToolbarPanel.add(toolbar, BorderLayout.NORTH); treeAndToolbarPanel.add(treeTableScrollPane, BorderLayout.CENTER); /* * outline / list tabbed pane */ outlineAndTreeTabbedPane = new JTabbedPane(JTabbedPane.BOTTOM); outlineAndTreeTabbedPane.add(treeAndToolbarPanel, "Outline"); outlineSorterJPanel = new OutlineSorterJPanel(); outlineAndTreeTabbedPane.add(outlineSorterJPanel, "Sorter"); outlineArchiveJPanel = new OutlineArchiveJPanel(); outlineAndTreeTabbedPane.add(outlineArchiveJPanel, "Archive"); /* * concept sidebar */ conceptJPanel = (ConceptJPanel) MindRaiderSpringContext.getCtx().getBean("conceptPanel"); /* * vertical split of notebook outline and RDF graph */ treeAndSpidersSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT); treeAndSpidersSplit.setContinuousLayout(true); treeAndSpidersSplit.setOneTouchExpandable(true); treeAndSpidersSplit.setDividerLocation(200); treeAndSpidersSplit.setLastDividerLocation(150); treeAndSpidersSplit.setDividerSize(6); treeAndSpidersSplit.add(outlineAndTreeTabbedPane); // spiders & tags visual navigation spidersAndTagsTabs = new JTabbedPane(JTabbedPane.BOTTOM); if (MindRaider.profile.isEnableSpiders()) { // notebook mind map spidersAndTagsTabs.addTab("Mind Map", MindRaider.spidersGraph.getPanel()); // TODO bundle } // global tags spidersAndTagsTabs.addTab("Tag Cloud", MindRaider.tagCustodian.getPanel()); // TODO bundle // global mind map //spidersAndTagsTabs.addTab("Global Mind Map",new JPanel()); // TODO bundle // lazy spiders rendering spidersAndTagsTabs.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource(); if (sourceTabbedPane.getSelectedIndex() == 0) { MindRaider.spidersGraph.renderModel(); } } }); if (!new ConfigurationBean().isDefaultTabMindMap()) { spidersAndTagsTabs.setSelectedIndex(1); } MindRaider.tagCustodian.redraw(); // add spiders panel treeAndSpidersSplit.add(spidersAndTagsTabs); /* * horizontal split of outline/graph slit and concept sidebar */ rightSiderbarSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeAndSpidersSplit, conceptJPanel); rightSiderbarSplitPane.setOneTouchExpandable(true); rightSiderbarSplitPane.setContinuousLayout(true); rightSiderbarSplitPane.setDividerLocation(500); rightSiderbarSplitPane.setLastDividerLocation(500); rightSiderbarSplitPane.setDividerSize(6); add(rightSiderbarSplitPane); }
From source file:com.mirth.connect.client.ui.browsers.message.MessageBrowserAdvancedFilter.java
private void initContentSearchTable() { contentSearchTable/*from ww w . j a va2 s. c om*/ .setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Content Type", "Contains" }) { public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } }); contentSearchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); contentSearchTable.setDragEnabled(false); contentSearchTable.setSortable(false); contentSearchTable.getTableHeader().setReorderingAllowed(false); contentSearchTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { deleteContentSearchButton.setEnabled(getSelectedRow(contentSearchTable) != -1); } }); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { contentSearchTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); } TableColumn column = contentSearchTable.getColumnModel().getColumn(0); column.setCellRenderer(new MirthComboBoxTableCellRenderer(ContentType.getDisplayValues())); column.setCellEditor(new MirthComboBoxTableCellEditor(contentSearchTable, ContentType.getDisplayValues(), 1, false, null)); column.setMinWidth(CONTENT_TYPE_COLUMN_WIDTH); column.setMaxWidth(CONTENT_TYPE_COLUMN_WIDTH); deleteContentSearchButton.setEnabled(false); }
From source file:com.mirth.connect.client.ui.browsers.message.MessageBrowserAdvancedFilter.java
private void initMetaDataSearchTable() { metaDataSearchTable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Metadata", "Operator", "Value", "Ignore Case" }) { public boolean isCellEditable(int rowIndex, int columnIndex) { if (columnIndex == 3 && cachedMetaDataColumns.get(getValueAt(rowIndex, 0)) .getType() != MetaDataColumnType.STRING) { return false; }/*from w ww . j av a 2s . c o m*/ return true; } @Override public void setValueAt(Object value, int row, int column) { int metaDataColumnIndex = findColumn("Metadata"); int operatorColumnIndex = findColumn("Operator"); int valueColumnIndex = findColumn("Value"); int ignoreCaseColumnIndex = findColumn("Ignore Case"); if (column == valueColumnIndex) { MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(getValueAt(row, metaDataColumnIndex)); if (StringUtils.isNotEmpty((String) value)) { try { metaDataColumn.getType().castValue(value); } catch (MetaDataColumnException e) { parent.alertError(parent, "Invalid value for column type " + metaDataColumn.getType().toString()); return; } } } else if (column == metaDataColumnIndex) { if (!value.equals(getValueAt(row, metaDataColumnIndex))) { MetaDataSearchOperator operator = MetaDataSearchOperator.EQUAL; super.setValueAt(operator, row, operatorColumnIndex); MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(value); if (metaDataColumn.getType() != MetaDataColumnType.STRING) { super.setValueAt(Boolean.FALSE, row, ignoreCaseColumnIndex); } } super.setValueAt("", row, valueColumnIndex); } super.setValueAt(value, row, column); } }); metaDataSearchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); metaDataSearchTable.setDragEnabled(false); metaDataSearchTable.setSortable(false); metaDataSearchTable.getTableHeader().setReorderingAllowed(false); addMetaDataSearchButton.setEnabled(!messageBrowser.getMetaDataColumns().isEmpty()); metaDataSearchTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { deleteMetaDataSearchButton.setEnabled(getSelectedRow(metaDataSearchTable) != -1); } }); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { metaDataSearchTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); } List<MetaDataColumn> metaDataColumns = messageBrowser.getMetaDataColumns(); cachedMetaDataColumns.clear(); String[] metaDataNames = new String[metaDataColumns.size()]; for (int i = 0; i < metaDataColumns.size(); i++) { String columnName = metaDataColumns.get(i).getName(); metaDataNames[i] = columnName; cachedMetaDataColumns.put(columnName, metaDataColumns.get(i)); } MirthComboBoxTableCellEditor metaDataEditor = new MirthComboBoxTableCellEditor(metaDataSearchTable, metaDataNames, 1, false, null); metaDataEditor.getComboBox().setAutoResizeDropdown(true); TableColumn metaDataColumn = metaDataSearchTable.getColumnModel().getColumn(0); metaDataColumn.setCellRenderer(new MirthComboBoxTableCellRenderer(metaDataNames)); metaDataColumn.setCellEditor(metaDataEditor); metaDataColumn.setMinWidth(METADATA_NAME_COLUMN_WIDTH); metaDataColumn.setMaxWidth(METADATA_NAME_COLUMN_WIDTH * 2); metaDataColumn.setPreferredWidth(METADATA_NAME_COLUMN_WIDTH); // Need to create this custom editor since the combo box values are dynamic based on metadata column type. MirthComboBoxTableCellEditor operatorEditor = new MirthComboBoxTableCellEditor(metaDataSearchTable, MetaDataSearchOperator.values(), 1, false, null) { public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(table.getValueAt(row, 0)); comboBox.setModel(new DefaultComboBoxModel( MetaDataSearchOperator.valuesForColumnType(metaDataColumn.getType()))); return super.getTableCellEditorComponent(table, value, isSelected, row, column); } }; TableColumn operatorColumn = metaDataSearchTable.getColumnModel().getColumn(1); operatorColumn.setCellRenderer(new MirthComboBoxTableCellRenderer(MetaDataSearchOperator.values())); operatorColumn.setCellEditor(operatorEditor); operatorColumn.setMinWidth(METADATA_OPERATOR_COLUMN_WIDTH); operatorColumn.setMaxWidth(METADATA_OPERATOR_COLUMN_WIDTH); TableColumn caseColumn = metaDataSearchTable.getColumnModel().getColumn(3); caseColumn.setMinWidth(METADATA_CASE_COLUMN_WIDTH); caseColumn.setMaxWidth(METADATA_CASE_COLUMN_WIDTH); deleteMetaDataSearchButton.setEnabled(false); }
From source file:net.sf.taverna.t2.activities.spreadsheet.views.SpreadsheetImportConfigView.java
@Override protected void initialise() { super.initialise(); newConfiguration = getJson().deepCopy(); // title// w ww . ja va2 s. c o m titlePanel = new JPanel(new BorderLayout()); titlePanel.setBackground(Color.WHITE); addDivider(titlePanel, SwingConstants.BOTTOM, true); titleLabel = new JLabel(SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.panelTitle")); titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 13.5f)); titleIcon = new JLabel(""); titleMessage = new DialogTextArea(DEFAULT_MESSAGE); titleMessage.setMargin(new Insets(5, 10, 10, 10)); // titleMessage.setMinimumSize(new Dimension(0, 30)); titleMessage.setFont(titleMessage.getFont().deriveFont(11f)); titleMessage.setEditable(false); titleMessage.setFocusable(false); // titleMessage.setFont(titleLabel.getFont().deriveFont(Font.PLAIN, // 12f)); // column range columnLabel = new JLabel( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.columnSectionLabel")); JsonNode columnRange = newConfiguration.get("columnRange"); columnFromValue = new JTextField(new UpperCaseDocument(), SpreadsheetUtils.getColumnLabel(columnRange.get("start").intValue()), 4); columnFromValue.setMinimumSize(columnFromValue.getPreferredSize()); columnToValue = new JTextField(new UpperCaseDocument(), SpreadsheetUtils.getColumnLabel(columnRange.get("end").intValue()), 4); columnToValue.setMinimumSize(columnToValue.getPreferredSize()); columnFromValue.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { checkValue(columnFromValue.getText()); } public void removeUpdate(DocumentEvent e) { checkValue(columnFromValue.getText()); } private void checkValue(String text) { if (text.trim().equals("")) { addErrorMessage(EMPTY_FROM_COLUMN_ERROR_MESSAGE); } else if (text.trim().matches("[A-Za-z]+")) { String fromColumn = columnFromValue.getText().toUpperCase(); String toColumn = columnToValue.getText().toUpperCase(); int fromColumnIndex = SpreadsheetUtils.getColumnIndex(fromColumn); int toColumnIndex = SpreadsheetUtils.getColumnIndex(toColumn); if (checkColumnRange(fromColumnIndex, toColumnIndex)) { columnMappingTableModel.setFromColumn(fromColumnIndex); columnMappingTableModel.setToColumn(toColumnIndex); newConfiguration.set("columnRange", newConfiguration.objectNode() .put("start", fromColumnIndex).put("end", toColumnIndex)); validatePortNames(); } removeErrorMessage(FROM_COLUMN_ERROR_MESSAGE); removeErrorMessage(EMPTY_FROM_COLUMN_ERROR_MESSAGE); } else { addErrorMessage(FROM_COLUMN_ERROR_MESSAGE); removeErrorMessage(EMPTY_FROM_COLUMN_ERROR_MESSAGE); } } }); columnToValue.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { checkValue(columnToValue.getText()); } public void removeUpdate(DocumentEvent e) { checkValue(columnToValue.getText()); } private void checkValue(String text) { if (text.trim().equals("")) { addErrorMessage(EMPTY_TO_COLUMN_ERROR_MESSAGE); } else if (text.trim().matches("[A-Za-z]+")) { String fromColumn = columnFromValue.getText().toUpperCase(); String toColumn = columnToValue.getText().toUpperCase(); int fromColumnIndex = SpreadsheetUtils.getColumnIndex(fromColumn); int toColumnIndex = SpreadsheetUtils.getColumnIndex(toColumn); if (checkColumnRange(fromColumnIndex, toColumnIndex)) { columnMappingTableModel.setFromColumn(fromColumnIndex); columnMappingTableModel.setToColumn(toColumnIndex); newConfiguration.set("columnRange", newConfiguration.objectNode() .put("start", fromColumnIndex).put("end", toColumnIndex)); validatePortNames(); } removeErrorMessage(TO_COLUMN_ERROR_MESSAGE); removeErrorMessage(EMPTY_TO_COLUMN_ERROR_MESSAGE); } else { addErrorMessage(TO_COLUMN_ERROR_MESSAGE); removeErrorMessage(EMPTY_TO_COLUMN_ERROR_MESSAGE); } } }); // row range rowLabel = new JLabel(SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.rowSectionLabel")); addDivider(rowLabel, SwingConstants.TOP, false); rowSelectAllOption = new JCheckBox( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.selectAllRowsOption")); rowExcludeFirstOption = new JCheckBox( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.excludeHeaderRowOption")); rowIgnoreBlankRows = new JCheckBox( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.ignoreBlankRowsOption")); rowSelectAllOption.setFocusable(false); rowExcludeFirstOption.setFocusable(false); JsonNode rowRange = newConfiguration.get("rowRange"); rowFromValue = new JTextField(new NumericDocument(), String.valueOf(rowRange.get("start").intValue() + 1), 4); if (rowRange.get("end").intValue() == -1) { rowToValue = new JTextField(new NumericDocument(), "", 4); } else { rowToValue = new JTextField(new NumericDocument(), String.valueOf(rowRange.get("end").intValue() + 1), 4); } rowFromValue.setMinimumSize(rowFromValue.getPreferredSize()); rowToValue.setMinimumSize(rowToValue.getPreferredSize()); if (newConfiguration.get("allRows").booleanValue()) { rowSelectAllOption.setSelected(true); rowFromValue.setEditable(false); rowFromValue.setEnabled(false); rowToValue.setEditable(false); rowToValue.setEnabled(false); } else { rowExcludeFirstOption.setEnabled(false); } rowExcludeFirstOption.setSelected(newConfiguration.get("excludeFirstRow").booleanValue()); rowIgnoreBlankRows.setSelected(newConfiguration.get("ignoreBlankRows").booleanValue()); rowFromValue.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { checkValue(rowFromValue.getText()); } public void removeUpdate(DocumentEvent e) { checkValue(rowFromValue.getText()); } private void checkValue(String text) { if (text.trim().equals("")) { addErrorMessage(EMPTY_FROM_ROW_ERROR_MESSAGE); } else if (text.trim().matches("[1-9][0-9]*")) { checkRowRange(rowFromValue.getText(), rowToValue.getText()); int fromRow = Integer.parseInt(rowFromValue.getText()); ((ObjectNode) newConfiguration.get("rowRange")).put("start", fromRow - 1); removeErrorMessage(FROM_ROW_ERROR_MESSAGE); removeErrorMessage(EMPTY_FROM_ROW_ERROR_MESSAGE); } else { addErrorMessage(FROM_ROW_ERROR_MESSAGE); removeErrorMessage(EMPTY_FROM_ROW_ERROR_MESSAGE); } } }); rowToValue.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { checkValue(rowToValue.getText()); } public void removeUpdate(DocumentEvent e) { checkValue(rowToValue.getText()); } private void checkValue(String text) { if (text.trim().equals("")) { ((ObjectNode) newConfiguration.get("rowRange")).put("end", -1); removeErrorMessage(TO_ROW_ERROR_MESSAGE); removeErrorMessage(INCONSISTENT_ROW_MESSAGE); } else if (text.trim().matches("[0-9]+")) { checkRowRange(rowFromValue.getText(), rowToValue.getText()); int toRow = Integer.parseInt(rowToValue.getText()); ((ObjectNode) newConfiguration.get("rowRange")).put("end", toRow - 1); removeErrorMessage(TO_ROW_ERROR_MESSAGE); } else { addErrorMessage(TO_ROW_ERROR_MESSAGE); } } }); rowSelectAllOption.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { newConfiguration.put("allRows", true); rowExcludeFirstOption.setEnabled(true); if (rowExcludeFirstOption.isSelected()) { rowFromValue.setText("2"); } else { rowFromValue.setText("1"); } rowToValue.setText(""); rowFromValue.setEditable(false); rowFromValue.setEnabled(false); rowToValue.setEditable(false); rowToValue.setEnabled(false); } else { newConfiguration.put("allRows", false); rowExcludeFirstOption.setEnabled(false); rowFromValue.setEditable(true); rowFromValue.setEnabled(true); rowToValue.setEditable(true); rowToValue.setEnabled(true); } } }); rowExcludeFirstOption.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { newConfiguration.put("excludeFirstRow", true); rowFromValue.setText("2"); ((ObjectNode) newConfiguration.get("rowRange")).put("start", 1); } else { newConfiguration.put("excludeFirstRow", false); rowFromValue.setText("1"); ((ObjectNode) newConfiguration.get("rowRange")).put("start", 0); } } }); rowIgnoreBlankRows.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { newConfiguration.put("ignoreBlankRows", e.getStateChange() == ItemEvent.SELECTED); } }); // empty cells emptyCellLabel = new JLabel( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.emptyCellSectionLabel")); addDivider(emptyCellLabel, SwingConstants.TOP, false); emptyCellButtonGroup = new ButtonGroup(); emptyCellEmptyStringOption = new JRadioButton( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.emptyStringOption")); emptyCellUserDefinedOption = new JRadioButton( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.userDefinedOption")); emptyCellErrorValueOption = new JRadioButton( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.generateErrorOption")); emptyCellEmptyStringOption.setFocusable(false); emptyCellUserDefinedOption.setFocusable(false); emptyCellErrorValueOption.setFocusable(false); emptyCellUserDefinedValue = new JTextField(newConfiguration.get("emptyCellValue").textValue()); emptyCellButtonGroup.add(emptyCellEmptyStringOption); emptyCellButtonGroup.add(emptyCellUserDefinedOption); emptyCellButtonGroup.add(emptyCellErrorValueOption); if (newConfiguration.get("emptyCellPolicy").textValue().equals("GENERATE_ERROR")) { emptyCellErrorValueOption.setSelected(true); emptyCellUserDefinedValue.setEnabled(false); emptyCellUserDefinedValue.setEditable(false); } else if (newConfiguration.get("emptyCellPolicy").textValue().equals("EMPTY_STRING")) { emptyCellEmptyStringOption.setSelected(true); emptyCellUserDefinedValue.setEnabled(false); emptyCellUserDefinedValue.setEditable(false); } else { emptyCellUserDefinedOption.setSelected(true); emptyCellUserDefinedValue.setText(newConfiguration.get("emptyCellValue").textValue()); emptyCellUserDefinedValue.setEnabled(true); emptyCellUserDefinedValue.setEditable(true); } emptyCellEmptyStringOption.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newConfiguration.put("emptyCellPolicy", "EMPTY_STRING"); emptyCellUserDefinedValue.setEnabled(false); emptyCellUserDefinedValue.setEditable(false); } }); emptyCellUserDefinedOption.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newConfiguration.put("emptyCellPolicy", "USER_DEFINED"); emptyCellUserDefinedValue.setEnabled(true); emptyCellUserDefinedValue.setEditable(true); emptyCellUserDefinedValue.requestFocusInWindow(); } }); emptyCellErrorValueOption.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newConfiguration.put("emptyCellPolicy", "GENERATE_ERROR"); emptyCellUserDefinedValue.setEnabled(false); emptyCellUserDefinedValue.setEditable(false); } }); emptyCellUserDefinedValue.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { newConfiguration.put("emptyCellValue", emptyCellUserDefinedValue.getText()); } public void insertUpdate(DocumentEvent e) { newConfiguration.put("emptyCellValue", emptyCellUserDefinedValue.getText()); } public void removeUpdate(DocumentEvent e) { newConfiguration.put("emptyCellValue", emptyCellUserDefinedValue.getText()); } }); // column mappings columnMappingLabel = new JLabel( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.columnMappingSectionLabel")); addDivider(columnMappingLabel, SwingConstants.TOP, false); Map<String, String> columnToPortMapping = new HashMap<>(); if (newConfiguration.has("columnNames")) { for (JsonNode columnName : newConfiguration.get("columnNames")) { columnToPortMapping.put(columnName.get("column").textValue(), columnName.get("port").textValue()); } } columnMappingTableModel = new SpreadsheetImportConfigTableModel(columnFromValue.getText(), columnToValue.getText(), columnToPortMapping); columnMappingTable = new JTable(); columnMappingTable.setRowSelectionAllowed(false); columnMappingTable.getTableHeader().setReorderingAllowed(false); columnMappingTable.setGridColor(Color.LIGHT_GRAY); // columnMappingTable.setFocusable(false); columnMappingTable.setColumnModel(new DefaultTableColumnModel() { public TableColumn getColumn(int columnIndex) { TableColumn column = super.getColumn(columnIndex); if (columnIndex == 0) { column.setMaxWidth(100); } return column; } }); TableCellEditor defaultEditor = columnMappingTable.getDefaultEditor(String.class); if (defaultEditor instanceof DefaultCellEditor) { DefaultCellEditor defaultCellEditor = (DefaultCellEditor) defaultEditor; defaultCellEditor.setClickCountToStart(1); Component editorComponent = defaultCellEditor.getComponent(); if (editorComponent instanceof JTextComponent) { final JTextComponent textField = (JTextComponent) editorComponent; textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { updateModel(textField.getText()); } public void insertUpdate(DocumentEvent e) { updateModel(textField.getText()); } public void removeUpdate(DocumentEvent e) { updateModel(textField.getText()); } private void updateModel(String text) { int row = columnMappingTable.getEditingRow(); int column = columnMappingTable.getEditingColumn(); columnMappingTableModel.setValueAt(text, row, column); ArrayNode columnNames = newConfiguration.arrayNode(); Map<String, String> columnToPortMapping = columnMappingTableModel.getColumnToPortMapping(); for (Entry<String, String> entry : columnToPortMapping.entrySet()) { columnNames.add(newConfiguration.objectNode().put("column", entry.getKey()).put("port", entry.getValue())); } newConfiguration.put("columnNames", columnNames); validatePortNames(); } }); } } columnMappingTable.setModel(columnMappingTableModel); // output format outputFormatLabel = new JLabel( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.outputFormatSectionLabel")); outputFormatMultiplePort = new JRadioButton( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.multiplePortOption")); outputFormatSinglePort = new JRadioButton( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.singlePortOption")); outputFormatMultiplePort.setFocusable(false); outputFormatSinglePort.setFocusable(false); outputFormatDelimiterLabel = new JLabel( SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.userDefinedCsvDelimiter")); outputFormatDelimiter = new JTextField(newConfiguration.get("csvDelimiter").textValue(), 5); outputFormatButtonGroup = new ButtonGroup(); outputFormatButtonGroup.add(outputFormatMultiplePort); outputFormatButtonGroup.add(outputFormatSinglePort); if (newConfiguration.get("outputFormat").textValue().equals("PORT_PER_COLUMN")) { outputFormatMultiplePort.setSelected(true); outputFormatDelimiterLabel.setEnabled(false); outputFormatDelimiter.setEnabled(false); } else { outputFormatSinglePort.setSelected(true); columnMappingLabel.setEnabled(false); enableTable(columnMappingTable, false); } outputFormatMultiplePort.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { outputFormatDelimiterLabel.setEnabled(false); outputFormatDelimiter.setEnabled(false); columnMappingLabel.setEnabled(true); enableTable(columnMappingTable, true); newConfiguration.put("outputFormat", "PORT_PER_COLUMN"); } }); outputFormatSinglePort.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { outputFormatDelimiterLabel.setEnabled(true); outputFormatDelimiter.setEnabled(true); columnMappingLabel.setEnabled(false); enableTable(columnMappingTable, false); newConfiguration.put("outputFormat", "SINGLE_PORT"); } }); outputFormatDelimiter.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { handleUpdate(); } public void insertUpdate(DocumentEvent e) { handleUpdate(); } public void removeUpdate(DocumentEvent e) { handleUpdate(); } private void handleUpdate() { String text = null; try { text = StringEscapeUtils.unescapeJava(outputFormatDelimiter.getText()); } catch (RuntimeException re) { } if (text == null || text.length() == 0) { newConfiguration.put("csvDelimiter", ","); } else { newConfiguration.put("csvDelimiter", text.substring(0, 1)); } } }); // buttons nextButton = new JButton(SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.nextButton")); nextButton.setFocusable(false); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backButton.setVisible(true); nextButton.setVisible(false); cardLayout.last(contentPanel); } }); backButton = new JButton(SpreadsheetImportUIText.getString("SpreadsheetImportConfigView.backButton")); backButton.setFocusable(false); backButton.setVisible(false); backButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { nextButton.setVisible(true); backButton.setVisible(false); cardLayout.first(contentPanel); } }); buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); addDivider(buttonPanel, SwingConstants.TOP, true); removeAll(); layoutPanel(); }
From source file:display.containers.FileManager.java
private void setColumnWidth(int column, int width) { TableColumn tableColumn = table.getColumnModel().getColumn(column); if (width < 0) { // use the preferred width of the header.. JLabel label = new JLabel((String) tableColumn.getHeaderValue()); Dimension preferred = label.getPreferredSize(); // altered 10->14 as per camickr comment. width = (int) preferred.getWidth() + 14; }// ww w .j a va2 s . c o m tableColumn.setPreferredWidth(width); tableColumn.setMaxWidth(width); tableColumn.setMinWidth(width); }