📄 editcvpanel.java
字号:
/* * File: EditCVPanel.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.util.gui;import mpi.util.BasicControlledVocabulary;import mpi.util.CVEntry;import mpi.util.ControlledVocabulary;import java.awt.Dimension;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.border.TitledBorder;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.UndoableEditEvent;import javax.swing.undo.CannotRedoException;import javax.swing.undo.CannotUndoException;import javax.swing.undo.UndoManager;/** * DOCUMENT ME! * $Id: jalopy_gnu_src_dist.xml,v 1.3 2007/02/06 13:30:33 hasloe Exp $ * @author $Author: hasloe $ * @version $Revision: 1.3 $ */public class EditCVPanel extends JPanel implements ActionListener, ListSelectionListener { /** Empty string to fill UI elements when values/description are empty. */ private static final String EMPTY = ""; /** Holds value of property DOCUMENT ME! */ private static final int MOVE_BUTTON_SIZE = 24; /** Holds value of property DOCUMENT ME! */ private static final int MINIMAL_ENTRY_PANEL_WIDTH = 240; /** Holds value of property DOCUMENT ME! */ protected BasicControlledVocabulary cv; // internal caching fields /** Holds value of property DOCUMENT ME! */ protected CVEntry currentEntry; /** Holds value of property DOCUMENT ME! */ protected JButton addEntryButton; /** Holds value of property DOCUMENT ME! */ protected JButton changeEntryButton; /** Holds value of property DOCUMENT ME! */ protected JButton deleteEntryButton; /** Holds value of property DOCUMENT ME! */ protected JButton moveDownButton; /** Holds value of property DOCUMENT ME! */ protected JButton moveToBottomButton; /** Holds value of property DOCUMENT ME! */ protected JButton moveToTopButton; /** Holds value of property DOCUMENT ME! */ protected JButton moveUpButton; /** Holds value of property DOCUMENT ME! */ protected JButton redoButton; /** Holds value of property DOCUMENT ME! */ protected JButton undoButton; /** Holds value of property DOCUMENT ME! */ protected JLabel entryDescLabel; /** Holds value of property DOCUMENT ME! */ protected JLabel entryValueLabel; /** Holds value of property DOCUMENT ME! */ protected JLabel titleLabel; /** Holds value of property DOCUMENT ME! */ protected JList entryList; /** Holds value of property DOCUMENT ME! */ protected JTextField entryDescTextField; /** Holds value of property DOCUMENT ME! */ protected JTextField entryValueTextField; /** Holds value of property DOCUMENT ME! */ protected String invalidValueMessage = "Invalid value"; /** Holds value of property DOCUMENT ME! */ protected String valueExistsMessage = "Value exists"; private DefaultListModel entryListModel; // ui elements private UndoManager undoManager; /** * opens panel with no cv * */ public EditCVPanel() { this(null); } /** * opens panel with cv * @param cv Controlled Vocabulary */ public EditCVPanel(BasicControlledVocabulary cv) { undoManager = new UndoManager() { public void undoableEditHappened(UndoableEditEvent e) { super.undoableEditHappened(e); updateUndoRedoButtons(); } }; makeLayout(); entryList.addListSelectionListener(this); setControlledVocabulary(cv); } /** * sets (new) cv * @param cv Controlled Vocabulary */ public void setControlledVocabulary(BasicControlledVocabulary cv) { this.cv = cv; undoManager.discardAllEdits(); updateLabels(); resetViewer(); entryValueTextField.setEnabled(cv != null); entryDescTextField.setEnabled(cv != null); if (cv instanceof ControlledVocabulary) { ((ControlledVocabulary) cv).addUndoableEditListener(undoManager); undoButton.setVisible(true); redoButton.setVisible(true); } else { undoButton.setVisible(false); redoButton.setVisible(false); } } /** * The button actions. * * @param actionEvent the actionEvent */ public void actionPerformed(ActionEvent actionEvent) { Object source = actionEvent.getSource(); // check source equality if (source == entryValueTextField) { entryDescTextField.requestFocus(); } else if ((source == addEntryButton) || (source == entryDescTextField)) { addEntry(); } else if (source == changeEntryButton) { changeEntry(); } else if (source == deleteEntryButton) { deleteEntries(); } else if (source == moveToTopButton) { moveEntries(BasicControlledVocabulary.MOVE_TO_TOP); } else if (source == moveUpButton) { moveEntries(BasicControlledVocabulary.MOVE_UP); } else if (source == moveDownButton) { moveEntries(BasicControlledVocabulary.MOVE_DOWN); } else if (source == moveToBottomButton) { moveEntries(BasicControlledVocabulary.MOVE_TO_BOTTOM); } else if (source == undoButton) { undo(); } else if (source == redoButton) { redo(); } } /** * for test purposes. opens frame with this panel and a test controlled vocabulary * @param args no arguments */ public static void main(String[] args) { javax.swing.JFrame frame = new javax.swing.JFrame(); ControlledVocabulary cv = new ControlledVocabulary("Name 1", "Description 1"); cv.addEntry(new CVEntry("Entry 1", "Entry description 1")); cv.addEntry(new CVEntry("Entry 2", "Entry description 2")); cv.addEntry(new CVEntry("Entry 3", "Entry description 3")); JPanel p = new EditCVPanel(cv); frame.getContentPane().add(p); frame.pack(); frame.setVisible(true); } /** * Handles a change in the selection in the entry list. * * @param lse the list selection event */ public void valueChanged(ListSelectionEvent lse) { if (lse.getSource() == entryList) { updateEntryButtons(); updateTextFields(); } } /** * Adds an entry to the current CV. When checking the uniqueness of the * entry value, values are compared case sensitive. */ protected void addEntry() { if (cv == null) { return; } String entry = entryValueTextField.getText(); entry = entry.trim(); if (entry.length() == 0) { showWarningDialog(invalidValueMessage); return; } if (cv.containsValue(entry)) { showWarningDialog(valueExistsMessage); } else { String desc = entryDescTextField.getText(); if (desc != null) { desc = desc.trim(); } CVEntry newEntry = new CVEntry(entry, desc); cv.addEntry(newEntry); updateList(); //make text fields free for next input! setSelectedEntry(null); } } /** * Changes the value and/or description of an existing entry. Checks * whether the specified value is unique within the current * ControlledVocabulary. */ protected void changeEntry() { if (cv == null) { return; } String newValue = entryValueTextField.getText().trim(); if (newValue.length() == 0) { showWarningDialog(invalidValueMessage); entryValueTextField.setText((currentEntry != null) ? currentEntry.getValue() : ""); return; } String newDescription = entryDescTextField.getText().trim(); if (newValue.equals(currentEntry.getValue())) { if ((newDescription != null) && !newDescription.equals(currentEntry.getDescription())) { CVEntry newEntry = new CVEntry(newValue, newDescription); cv.replaceEntry(currentEntry, newEntry); updateList(); setSelectedEntry(newEntry); } return; } // entry value has changed... if (cv.containsValue(newValue)) { showWarningDialog(valueExistsMessage); } else { CVEntry newEntry = new CVEntry(newValue, newDescription); cv.replaceEntry(currentEntry, new CVEntry(newValue, newDescription)); updateList(); setSelectedEntry(newEntry); } } /** * Deletes the selected entry/entries from the current * ControlledVocabulary. */ protected void deleteEntries() { Object[] selEntries = entryList.getSelectedValues(); if (selEntries.length == 0) { return; } CVEntry[] entries = new CVEntry[selEntries.length]; for (int i = 0; i < entries.length; i++) { entries[i] = (CVEntry) selEntries[i]; } cv.removeEntries(entries); updateList(); setSelectedEntry(null); } /** * This method is called from within the constructor to initialize the * dialog's components. */ protected void makeLayout() { JPanel moveEntriesPanel; GridBagConstraints gridBagConstraints; ImageIcon topIcon = new ImageIcon(this.getClass().getResource("/mpi/util/gui/resources/Top16.gif")); ImageIcon bottomIcon = new ImageIcon(this.getClass().getResource("/mpi/util/gui/resources/Bottom16.gif")); ImageIcon upIcon = new ImageIcon(this.getClass().getResource("/toolbarButtonGraphics/navigation/Up16.gif")); ImageIcon downIcon = new ImageIcon(this.getClass().getResource("/toolbarButtonGraphics/navigation/Down16.gif")); ImageIcon redoIcon = new ImageIcon(this.getClass().getResource("/toolbarButtonGraphics/general/Redo16.gif")); ImageIcon undoIcon = new ImageIcon(this.getClass().getResource("/toolbarButtonGraphics/general/Undo16.gif"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -