📄 inlineeditbox.java
字号:
if (menuBar != null) { editorMenu.setText(ElanLocale.getString("InlineEditBox.Menu.Editor")); editMenu.setText(ElanLocale.getString("Menu.Edit")); selectLanguageMenu.setText(ElanLocale.getString( "InlineEditBox.Menu.Select")); attachMI.setText(ElanLocale.getString("InlineEditBox.Attach")); commitMI.setText(ElanLocale.getString("InlineEditBox.Commit")); cancelMI.setText(ElanLocale.getString("InlineEditBox.Cancel")); cutMI.setText(ElanLocale.getString("InlineEditBox.Edit.Cut")); copyMI.setText(ElanLocale.getString("InlineEditBox.Edit.Copy")); pasteMI.setText(ElanLocale.getString("InlineEditBox.Edit.Paste")); } } ////////////////////////// // inner class: a component for selecting an entry from a list of entries of a cv ////////////////////////// /** * A class that provides a component for the selection of an entry from a ControlledVocabulary.<br> * The current possible delegate components are a JScrollPane containing * a JList or a JComboBox * * @author Han Sloetjes */ class CVEntryComponent implements KeyListener, ActionListener { /** the list containing the cv entries */ private JList entryList; /** the model for the list */ private DefaultListModel entryListModel; /** the scrollpane for the list */ private JScrollPane scrollPane; /** popup menu for detach, commit and cancel */ private JPopupMenu popup; /** moudelistener for bringing up the popup menu */ private MouseListener popupListener; /** mouse listener that handles a double click on a list entry */ private MouseListener doubleClickListener; /** menu items for detaching, cancelling and committing */ private JMenuItem detachMI; /** menu items for detaching, cancelling and committing */ private JMenuItem cancelMI; /** menu items for detaching, cancelling and committing */ private JMenuItem commitMI; /** a combo box editor component */ private JComboBox box; /** a model for the combo box */ private DefaultComboBoxModel entryBoxModel; /** * the component to use for editing, either a scrollpane containing a * JList or a JComboBox */ private JComponent delegate; /** * the array of CV entries from the ControlledVocabulary referenced by * the LinguisticType in use by the Tier containing the current * annotation */ private CVEntry[] entries; /** the annotation to edit */ private Annotation annotation; /** * Creates a new entrylist and initializes components.<br> * Components are being initialized depending on the type of the * argument. * * @param componentClass the type of component to use for edit * operations */ public CVEntryComponent(Class componentClass) { initComponents(componentClass); } /** * Returns the current delegate component. * * @return the delegate component for editing actions */ public JComponent getEditorComponent() { return delegate; } /** * Sets which type of component should be used for editing. Can depend * on the kind of viewer that created the InlineEditBox and of the * attached / detached state. * * @param compClass the type of component to use for editing */ void setDelegate(Class compClass) { if (delegate.getClass() == compClass) { return; } if (compClass == JComboBox.class) { if (box == null) { initComponents(compClass); } delegate = box; // make sure it is filled with the current entries entryBoxModel.removeAllElements(); fillModel(); if (entryList != null) { box.setSelectedItem(entryList.getSelectedValue()); } } else if (compClass == JScrollPane.class) { if (entryList == null) { initComponents(compClass); } delegate = scrollPane; entryListModel.clear(); fillModel(); if (box != null) { entryList.setSelectedValue(box.getSelectedItem(), true); } } } /** * Tries to ensure that the selected item is visible in the * scrollpane's viewport. Applies only to the JList component. */ public void ensureSelectionIsVisible() { if (delegate instanceof JScrollPane && (entryList != null)) { entryList.ensureIndexIsVisible(entryList.getSelectedIndex()); } } /** * When this list is in a detached dialog it doesn't need the popup * because all options are in the menu bar. Applies only to the JList * component. */ public void removePopupListener() { if (entryList != null) { entryList.removeMouseListener(popupListener); } } /** * When this list is not in a dialog menu items for detaching, * committing and cancelling need to be provided. Applies only to * the JList component. */ public void addPopupListener() { if (entryList != null) { MouseListener[] listeners = entryList.getMouseListeners(); for (int i = 0; i < listeners.length; i++) { if (listeners[i] == popupListener) { return; } } entryList.addMouseListener(popupListener); } } /** * Initializes either a list in a scrollpane (with a popup menu etc) or * a combo box. Adds listeners. * * @param component the type of component to use for editing */ private void initComponents(Class component) { if (component == JScrollPane.class) { if (entryList == null) { entryListModel = new DefaultListModel(); entryList = new JList(entryListModel); entryList.setFont(InlineEditBox.this.getFont()); entryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollPane = new JScrollPane(entryList); popup = new JPopupMenu(); detachMI = new JMenuItem(ElanLocale.getString( "InlineEditBox.Detach")); detachMI.addActionListener(this); detachMI.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, ActionEvent.SHIFT_MASK)); popup.add(detachMI); commitMI = new JMenuItem(ElanLocale.getString( "InlineEditBox.Commit")); commitMI.addActionListener(this); commitMI.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); popup.add(commitMI); cancelMI = new JMenuItem(ElanLocale.getString( "InlineEditBox.Cancel")); cancelMI.addActionListener(this); cancelMI.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0)); popup.add(cancelMI); popupListener = new MouseAdapter() { public void mousePressed(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger()) { CVEntryComponent.this.popup.show(CVEntryComponent.this.entryList, e.getX(), e.getY()); CVEntryComponent.this.popup.setVisible(true); } } }; doubleClickListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 1) { InlineEditBox.this.commitEdit(); } } }; entryList.addMouseListener(popupListener); entryList.addMouseListener(doubleClickListener); entryList.addKeyListener(this); entryList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent lse) { CVEntryComponent.this.ensureSelectionIsVisible(); } }); delegate = scrollPane; } } else if (component == JComboBox.class) { if (box == null) { entryBoxModel = new DefaultComboBoxModel(); box = new JComboBox(entryBoxModel); box.addActionListener(this); box.addKeyListener(this); delegate = box; } } } /** * Sets the font for the entry list component. * * @param f the font */ public void setFont(Font f) { if (delegate == box) { box.setFont(f); } else if (delegate == scrollPane) { entryList.setFont(f); } } /** * Gets the entry array with the entries in the cv referenced by the * linguistic type of the tier. * * @param annotation the active annotation */ public void setAnnotation(Annotation annotation) { this.annotation = annotation; if (entryListModel != null) { entryListModel.clear(); } if (entryBoxModel != null) { entryBoxModel.removeAllElements(); } if (annotation != null) { TierImpl tier = (TierImpl) annotation.getTier(); TranscriptionImpl trans = (TranscriptionImpl) tier.getParent(); ControlledVocabulary cv = trans.getControlledVocabulary(tier.getLinguisticType() .getControlledVocabylaryName()); if (cv != null) { entries = cv.getEntries(); } } fillModel(); } /** * Fills the model of either the combo box or the list with the entries * of the current Controlled Vocabulary. */ private void fillModel() { String value = null; if (annotation != null) { value = annotation.getValue(); } if (delegate == scrollPane) { for (int i = 0; i < entries.length; i++) { entryListModel.addElement(entries[i]); if ((value != null) && value.equals(entries[i].getValue())) { entryList.setSelectedIndex(i); } } if ((entries.length > 0) && (entryList.getSelectedIndex() < 0)) { entryList.setSelectedIndex(0); } } else if (delegate == box) { for (int i = 0; i < entries.length; i++) { entryBoxModel.addElement(entries[i]); if ((value != null) && value.equals(entries[i].getValue())) { entryBoxModel.setSelectedItem(entries[i]); } } if ((entries.length > 0) && (box.getSelectedIndex() < 0)) { box.setSelectedIndex(0); } } } /** * Tries to grant the focus to the delegate component. */ public void grabFocus() { if (delegate == box) { box.requestFocus(); } else if (delegate == scrollPane) { entryList.requestFocus(); entryList.ensureIndexIsVisible(entryList.getSelectedIndex()); } } /** * Returns the currently selected entry value. * * @return the currently selected entry value or null */ public String getSelectedEntryValue() { String value = null; if (delegate == scrollPane) { if (entryList.getSelectedValue() != null) { value = ((CVEntry) entryList.getSelectedValue()).getValue(); } } else if (delegate == box) { if (box.getSelectedItem() != null) { value = ((CVEntry) box.getSelectedItem()).getValue(); } else { value = annotation.getValue(); } } return value; } /** * KeyPressed handling. * * @param e the key event */ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.isShiftDown()) { InlineEditBox.this.detachEditor(); } /*else if ( (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0){ InlineEditBox.this.commitEdit(); } */ else { // in all other cases commit when the Enter key is typed InlineEditBox.this.commitEdit(); } } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { InlineEditBox.this.cancelEdit(); } } /** * Key released handling: do nothing. * * @param e the key event */ public void keyReleased(KeyEvent e) { } /** * Key typed handling: do nothing. * * @param e the key event */ public void keyTyped(KeyEvent e) { } /** * Action handling. * * @param ae the action event */ public void actionPerformed(ActionEvent ae) { if (ae.getSource() == detachMI) { if (attachable) { if (attached) { InlineEditBox.this.detachEditor(); } else { InlineEditBox.this.attachEditor(); } } } else if (ae.getSource() == commitMI) { InlineEditBox.this.commitEdit(); } else if (ae.getSource() == cancelMI) { InlineEditBox.this.cancelEdit(); } else if (ae.getSource() == box) { if ((ae.getID() == ActionEvent.ACTION_PERFORMED) && (ae.getModifiers() == InputEvent.BUTTON1_MASK)) { // prevent that the first click / doubleclick on the combo box // causes a commit if (box.isPopupVisible()) { InlineEditBox.this.commitEdit(); } } } } } // end of CVEntryComponent}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -