📄 xmlbasictablepanel.java
字号:
jc.endUndouableChange(sel); ipc.getPanelSettings().adjustActions(); adjustActions(); } } } }; protected Action moveUpAction = new AbstractAction("MoveUp") { public void actionPerformed(ActionEvent ae) { moveItem(0); adjustActions(); } }; protected Action moveDownAction = new AbstractAction("MoveDown") { public void actionPerformed(ActionEvent ae) { moveItem(1); adjustActions(); } }; protected Vector getColumnNames(List columnsToShow) { // creating a table which do not allow cell editing Vector cnames = new Vector(); cnames.add("Object"); XMLElement cel = ((XMLCollection) getOwner()).generateNewElement(); if (cel instanceof XMLComplexElement) { Iterator it = columnsToShow.iterator(); while (it.hasNext()) { String elemName = (String) it.next(); XMLElement el = ((XMLComplexElement) cel).get(elemName); if (el != null) { cnames.add(JaWEManager.getInstance().getLabelGenerator().getLabel(el)); } else { it.remove(); } } } else { cnames.add(JaWEManager.getInstance().getLabelGenerator().getLabel(cel)); } return cnames; } protected JTable createTable(final boolean colors) { JTable t=new BasicSortingTable(this, new Vector(), columnNames) { public boolean isCellEditable(int row, int col) { return false; } // This table colors elements depending on their owner public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) { Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); if (!isCellSelected(rowIndex, vColIndex) && colors) { XMLElement el = (XMLElement) getValueAt(rowIndex, 0); if (el instanceof XMLCollectionElement) { XMLCollectionElement cel = (XMLCollectionElement) el; XMLCollection celOwner = (XMLCollection) cel.getParent(); if (celOwner == null) { c.setBackground(SPEC_EL_COLOR_BKG); } else if (celOwner!=getOwner()) { c.setBackground(FOREIGN_EL_COLOR_BKG); } else { c.setBackground(getBackground()); } } else { c.setBackground(getBackground()); } } return c; } }; Color bkgCol=new Color(245,245,245); if (ipc.getSettings() instanceof PanelSettings) { bkgCol=((PanelSettings)ipc.getSettings()).getBackgroundColor(); } t.setBackground(bkgCol); return t; } protected void setupTable(boolean miniDimension, boolean automaticWidth, final boolean showArrows) { TableColumn column; // setting the first column (object column) to be invisible column = allItems.getColumnModel().getColumn(0); column.setMinWidth(0); column.setMaxWidth(0); column.setPreferredWidth(0); column.setResizable(false); // setting fields that will not be displayed within the table // setting some table properties allItems.setColumnSelectionAllowed(false); allItems.setRowSelectionAllowed(true); allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); allItems.getTableHeader().setReorderingAllowed(false); Dimension tDim; int noOfVisibleColumns = columnNames.size() - 1; if (miniDimension) { tDim = new Dimension(miniTableDimension); } else if (noOfVisibleColumns <= 3) { tDim = new Dimension(smallTableDimension); } else if (noOfVisibleColumns <= 5) { tDim = new Dimension(mediumTableDimension); } else { tDim = new Dimension(largeTableDimension); } if (automaticWidth) { tDim.width = allItems.getPreferredScrollableViewportSize().width; } allItems.setPreferredScrollableViewportSize(new Dimension(tDim)); allItems.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "edit"); allItems.getActionMap().put("edit", editElementAction); allItems.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false), "delete"); allItems.getActionMap().put("delete", deleteElementAction); final XMLCollection col = (XMLCollection) getOwner(); final boolean canRepos = JaWEManager.getInstance().getJaWEController().canRepositionElement(col, null); if (!getOwner().isReadOnly()) allItems.setToolTipText(ResourceManager.getLanguageDependentString("MessageDragItemToChangeItsPosition")); // mouse listener for editing on double-click allItems.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if (me.getClickCount() > 1) { editElementAction.actionPerformed(null); } } /** Marks the object which place within the table will be changed.*/ public void mousePressed(MouseEvent me) { movingElement = null; movingElementPosition = -1; if (showArrows && !getOwner().isReadOnly() && canRepos) { dragging = true; } try { movingElementPosition = allItems.getSelectedRow(); if (movingElementPosition >= 0) { movingElement = (XMLElement) allItems.getValueAt(movingElementPosition, 0); adjustActions(); } } catch (Exception ex) { } } /** Just indicates that dragging is over.*/ public void mouseReleased(MouseEvent me) { dragging = false; } }); /** Changes position of object within the list.*/ if (showArrows && !myOwner.isReadOnly() && canRepos) {// && ((XMLCollection)getOwner()).getParent().isReadOnly()))) { ListSelectionModel rowSM = allItems.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent lse) { if (dragging && !changing) { newMovingElementPosition = -1; try { newMovingElementPosition = allItems.getSelectedRow(); } catch (Exception ex) { } moveItem(); } adjustActions(); } }); } } protected void fillTableContent(List elementsToShow) { DefaultTableModel dtm = (DefaultTableModel) allItems.getModel(); Iterator it = elementsToShow.iterator(); while (it.hasNext()) { XMLElement elem = (XMLElement) it.next(); Vector v = getRow(elem); dtm.addRow(v); } } protected Vector getRow(XMLElement elem) { Vector v = new Vector(); if (elem instanceof XMLComplexElement) { Iterator itAllElems = columnsToShow.iterator(); v = new Vector(); XMLComplexElement cmel=(XMLComplexElement) elem; while (itAllElems.hasNext()) { String elName=(String)itAllElems.next(); XMLElement el = cmel.get(elName); if (el!=null) { v.add(new XMLElementView(ipc,el, XMLElementView.TOVALUE)); } } } else { v.add(new XMLElementView(ipc,elem, XMLElementView.TOVALUE)); } v.add(0, elem); return v; } protected JScrollPane createScrollPane() { // creates panel JScrollPane allItemsPane = new JScrollPane(); allItemsPane.setViewportView(allItems); return allItemsPane; } protected JPanel createToolbar() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JButton buttonNew = PanelUtilities.createToolbarButton(ipc.getSettings(),newElementAction); buttonNew.setRolloverEnabled(true); JButton buttonEdit = PanelUtilities.createToolbarButton(ipc.getSettings(),editElementAction); buttonEdit.setRolloverEnabled(true); JButton buttonDelete = PanelUtilities.createToolbarButton(ipc.getSettings(), deleteElementAction); buttonDelete.setRolloverEnabled(true); panel.add(buttonNew); panel.add(Box.createRigidArea(new Dimension(3, 3))); panel.add(buttonEdit); panel.add(Box.createRigidArea(new Dimension(3, 3))); panel.add(buttonDelete); panel.add(Box.createHorizontalGlue()); return panel; } protected JPanel createArrowPanel() { JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); JButton buttonUp = new JButton(); buttonUp.setIcon(ipc.getPanelSettings().getArrowUpImageIcon()); buttonUp.setPreferredSize(new Dimension(16, 16)); buttonUp.setEnabled(false); buttonUp.addActionListener(moveUpAction); moveUpAction.addPropertyChangeListener(new ButtonPropertyChangedListener(buttonUp)); JButton buttonDown = new JButton(); buttonDown.setIcon(ipc.getPanelSettings().getArrowDownImageIcon()); buttonDown.setPreferredSize(new Dimension(16, 16)); buttonDown.setEnabled(false); buttonDown.addActionListener(moveDownAction); moveDownAction.addPropertyChangeListener(new ButtonPropertyChangedListener(buttonDown)); p.add(buttonUp); p.add(Box.createVerticalGlue()); p.add(buttonDown); return p; } public void xmlElementChanged(XMLElementChangeInfo info) { if (info.getAction() == XMLElementChangeInfo.REMOVED) { Iterator it = info.getChangedSubElements().iterator(); while (it.hasNext()) { XMLElement el = (XMLElement) it.next(); int row = getElementRow(el);// System.out.println("Removing row " + row + " for element " + el); if (row != -1) { removeRow(row); } } } else if (info.getAction() == XMLElementChangeInfo.INSERTED) { Iterator it = info.getChangedSubElements().iterator(); while (it.hasNext()) { XMLElement el = (XMLElement) it.next(); addRow(el); } } } protected int getElementRow(XMLElement el) { int row = -1; for (int i = 0; i < allItems.getRowCount(); i++) { XMLElement toCompare = (XMLElement) allItems.getValueAt(i, 0); if (el==toCompare) { row = i; break; } } return row; } protected void adjustActions() { JaWEController jc = JaWEManager.getInstance().getJaWEController(); XMLElement selEl=getSelectedElement(); newElementAction.setEnabled(jc.canCreateElement((XMLCollection) getOwner())); editElementAction.setEnabled((selEl != null && XMLUtil.getPackage(selEl)!=null)); deleteElementAction.setEnabled((selEl != null && jc.canRemoveElement((XMLCollection)getOwner(), selEl))); boolean canRepos = JaWEManager.getInstance().getJaWEController().canRepositionElement((XMLCollection) getOwner(), null); moveUpAction.setEnabled(selEl != null && allItems.getSelectedRow() > 0 && canRepos); moveDownAction.setEnabled(selEl != null && allItems.getSelectedRow() < allItems.getModel().getRowCount() - 1 && canRepos); } public void cleanup () { myOwner.removeListener(this); } public boolean isEmpty () { return allItems.getRowCount()==0; } public boolean validateEntry () { if (isEmpty() && getOwner().isRequired() && !getOwner().isReadOnly()) { XMLBasicPanel.defaultErrorMessage(this.getWindow(),getTitle()+": "); allItems.requestFocus(); return false; } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -