📄 attrdialog.java
字号:
* Dismisses window without apply changes. */ class DismissActionHandler implements ActionListener { public void actionPerformed(ActionEvent evt) { // Get rid of window AttrDialog.this.dispose(); } } /** * Reinitializes table using original attributes with * which this Attribute Dialog window was created. * Any edits are lost. */ class ReloadActionHandler implements ActionListener { public void actionPerformed(ActionEvent evt) { // Forget about any edits, revert to original. // More draconian approach would be to load from directory // (i.e. call getAttributes() again). // Replace data tableModel = new AttrTableModel(orig); table.setModel(tableModel); table.getColumn(heading[1]).setCellRenderer(renderer); // Notify table of change table.tableChanged(new TableModelEvent(tableModel)); // Disable Apply button (no change yet) apply.setEnabled(false); } } /** * Model that represents data of AttrDialog table. * It represents the attributes as two Vectors, one for attribute * ids and one for attribute values. */ class AttrTableModel extends AbstractTableModel { Vector ids, values; AttrTableModel(Attributes attrs) { toVectors(attrs); } // Convert Attributes into two vectors (ids and values) void toVectors(Attributes attrs) { if (attrs == null || attrs.size() == 0) { ids = new Vector(10); values = new Vector(10); return; } try { // Just approximation, since there can be multivalued attributes. ids = new Vector(attrs.size()); values = new Vector(attrs.size()); Attribute attr; for(NamingEnumeration enum = attrs.getAll(); enum.hasMore();) { attr = (Attribute)enum.next(); if (attr.size() > 1) { String id = attr.getID(); // Multivalued. for (NamingEnumeration vals = attr.getAll(); vals.hasMore();) { ids.addElement(id); values.addElement(vals.next()); } } else { ids.addElement(attr.getID()); try { values.addElement(attr.get()); } catch (NamingException e) { if (debug) e.printStackTrace(); values.addElement(null); } } } } catch (NamingException e) { if (debug) e.printStackTrace(); setStatus(e); } } // Convert table model into an Attributes Attributes toAttributes() throws InvalidAttributeIdentifierException { int count = ids.size(); Attributes attrs = new BasicAttributes(true); // caseIgnore Attribute attr; String id; Object value; for (int i = 0; i < count; i++) { id = (String)ids.elementAt(i); if (id.equals("")) { throw new InvalidAttributeIdentifierException("Empty attribute id"); } value = values.elementAt(i); if (value instanceof String) { if (((String)value).equals("")) { value = null; // assume empty string denotes null } } if ((attr = attrs.get(id)) != null && attr.size() > 0) { attr.add(value); } else { attrs.put(new BasicAttribute(id, value)); } } return attrs; } public int getColumnCount() { return heading.length; } public int getRowCount() { return ids.size(); } /** * Returns the attribute id or value in the table. * Do special casing for jpegPhoto, audio and binary attributes: * jpegPhoto: Render an image described by the attribute value * audio: Return a "speaker" icon which when selected will * play the audio. * binary: all other binary attributes are indicated by the * label "** binary **" * * Note that the classification of attribute values uses * its attribute id. One could use the schema, but that * many services do not support schema. * * Note also that the image displayed is shrunken to fit * into the table. When you click on the attribute, the * image is displayed in its original size. * An alternative is to not shrink the image * and provide a slider for increasing the table's row height. * (There is no way to control individual row heights in JTable.) */ public Object getValueAt(int row, int col) { if (col == 0) { // attribute ID return ids.elementAt(row); } else { // attribute value Object obj = values.elementAt(row); if (obj instanceof String) { return obj; } // If value is not String (i.e. binary) // treat try to render jpegPhoto, // and create voice button for audio. // All others, just say "** binary **". String id = (String)ids.elementAt(row); if (id.equalsIgnoreCase("jpegPhoto")) { // Get image and shrink to fit within row ImageIcon orig = new ImageIcon((byte[])obj); Image im = orig.getImage(); Image shrunken = im.getScaledInstance(-1, table.getRowHeight(), Image.SCALE_DEFAULT); orig.setImage(shrunken); return orig; } else if (id.equalsIgnoreCase("audio")) { return Browser.speakerIcon; } else { return "** binary **"; } } } public String getColumnName(int column) { return heading[column]; } public boolean isCellEditable(int row, int col) { if (col == 0) return true; // attribute ID is always editable else { // only String values are editable Object obj = values.elementAt(row); return (obj instanceof String); } } public void setValueAt(Object aValue, int row, int col) { if (col == 0) { // Modifying attribute ID ids.setElementAt(aValue, row); } else { values.setElementAt(aValue, row); } apply.setEnabled(true); } public int addEmptyRow() { ids.addElement(""); values.addElement(""); if (debug) System.err.println("Added: " + (ids.size() - 1)); return ids.size() - 1 ; } public int[] removeRows() { // Before removal, stop editing table.editingStopped(new ChangeEvent(table)); int[] selected = table.getSelectedRows(); // returned array is always ordered // from lowest to highest index // start from back so deletions do not change indices // (deleting an element from a Vector shifts the indices) for (int i = selected.length-1; i >= 0; i--) { if (debug) { System.err.println("removing " + selected[i] + ":" + ids.elementAt(selected[i])); } ids.removeElementAt(selected[i]); values.removeElementAt(selected[i]); } return selected; } }; /** * Renderer for cells in AttrDialog table. * Each cell can be rendered as an text label or an icon. * (JTable's default will choose a renderer for a column based on * the first entry in a column. Our renderer will allow * icons to appear anywhere in the table and be rendered as such * without depending on the top entry in the column. */ class AttrCellRenderer extends DefaultTableCellRenderer { public void setValue(Object value) { if (value instanceof Icon) { Icon icon = (Icon)value; setIcon(icon); setText(""); } else { setIcon(null); super.setValue(value); } } } /** * If audio attribute has been selected, play its associated * attribute value as an audio stream. */ class AudioVideoChecker implements ListSelectionListener { public void valueChanged(ListSelectionEvent evt) { int first = evt.getFirstIndex(); int last = evt.getLastIndex(); if (first != last || first < 0) return; String id = (String)tableModel.ids.elementAt(first); if (id.equalsIgnoreCase("audio")) { byte[] soundBytes = (byte[]) tableModel.values.elementAt(first); // Play audio try { AudioStream as = new AudioStream( new ByteArrayInputStream(soundBytes)); AudioPlayer.player.start(as); } catch (IOException e) { System.err.println("Cannot play audio"); } } else if (id.equalsIgnoreCase("jpegPhoto")) { byte[] bytes= (byte[]) tableModel.values.elementAt(first); JOptionPane.showMessageDialog(AttrDialog.this, new JLabel(new ImageIcon(bytes)), "jpegPhoto", JOptionPane.PLAIN_MESSAGE); } } } /** * Methods for handling status line of AttrDialog. */ void setStatus(String s, boolean beep) { if (beep) java.awt.Toolkit.getDefaultToolkit().beep(); status.setText(s); } void setStatus(String s) { setStatus(s, true); } void setStatus(Exception e) { setStatus(e.getClass().getName()); Browser.appendToConsole(e); } void resetStatus() { status.setText("Ready"); } /** * Methods for handling stop watch cursor. */ Cursor savedCursor; void waitCursor() { savedCursor = getCursor(); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); } void restoreCursor() { setCursor(savedCursor); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -