📄 arffviewermainpanel.java
字号:
/* * 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., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * ArffViewerMainPanel.java * Copyright (C) 2005 FracPete * */package weka.gui.arffviewer;import weka.core.Capabilities;import weka.core.Instances;import weka.core.converters.AbstractSaver;import weka.gui.ComponentHelper;import weka.gui.ConverterFileChooser;import weka.gui.JTableHelper;import weka.gui.ListSelectorDialog;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Cursor;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.WindowEvent;import java.io.File;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.Vector;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JInternalFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTabbedPane;import javax.swing.KeyStroke;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;/** * The main panel of the ArffViewer. It has a reference to the menu, that an * implementing JFrame only needs to add via the setJMenuBar(JMenuBar) method. * * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.5 $ */public class ArffViewerMainPanel extends JPanel implements ActionListener, ChangeListener { /** for serialization */ static final long serialVersionUID = -8763161167586738753L; /** the default for width */ public final static int DEFAULT_WIDTH = -1; /** the default for height */ public final static int DEFAULT_HEIGHT = -1; /** the default for left */ public final static int DEFAULT_LEFT = -1; /** the default for top */ public final static int DEFAULT_TOP = -1; /** default width */ public final static int WIDTH = 800; /** default height */ public final static int HEIGHT = 600; protected Component parent; protected JTabbedPane tabbedPane; protected JMenuBar menuBar; protected JMenu menuFile; protected JMenuItem menuFileOpen; protected JMenuItem menuFileSave; protected JMenuItem menuFileSaveAs; protected JMenuItem menuFileClose; protected JMenuItem menuFileCloseAll; protected JMenuItem menuFileProperties; protected JMenuItem menuFileExit; protected JMenu menuEdit; protected JMenuItem menuEditUndo; protected JMenuItem menuEditCopy; protected JMenuItem menuEditSearch; protected JMenuItem menuEditClearSearch; protected JMenuItem menuEditDeleteAttribute; protected JMenuItem menuEditDeleteAttributes; protected JMenuItem menuEditRenameAttribute; protected JMenuItem menuEditAttributeAsClass; protected JMenuItem menuEditDeleteInstance; protected JMenuItem menuEditDeleteInstances; protected JMenuItem menuEditSortInstances; protected JMenu menuView; protected JMenuItem menuViewAttributes; protected JMenuItem menuViewValues; protected JMenuItem menuViewOptimalColWidths; protected ConverterFileChooser fileChooser; protected String frameTitle; protected boolean confirmExit; protected int width; protected int height; protected int top; protected int left; protected boolean exitOnClose; /** * initializes the object * * @param parentFrame the parent frame (JFrame or JInternalFrame) */ public ArffViewerMainPanel(Component parentFrame) { parent = parentFrame; frameTitle = "ARFF-Viewer"; createPanel(); } /** * creates all the components in the panel */ protected void createPanel() { // basic setup setSize(WIDTH, HEIGHT); setConfirmExit(false); setLayout(new BorderLayout()); // file dialog fileChooser = new ConverterFileChooser(new File(System.getProperty("user.dir"))); fileChooser.setMultiSelectionEnabled(true); // menu menuBar = new JMenuBar(); menuFile = new JMenu("File"); menuFileOpen = new JMenuItem("Open...", ComponentHelper.getImageIcon("open.gif")); menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK)); menuFileOpen.addActionListener(this); menuFileSave = new JMenuItem("Save", ComponentHelper.getImageIcon("save.gif")); menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK)); menuFileSave.addActionListener(this); menuFileSaveAs = new JMenuItem("Save as...", ComponentHelper.getImageIcon("empty.gif")); menuFileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)); menuFileSaveAs.addActionListener(this); menuFileClose = new JMenuItem("Close", ComponentHelper.getImageIcon("empty.gif")); menuFileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_MASK)); menuFileClose.addActionListener(this); menuFileCloseAll = new JMenuItem("Close all", ComponentHelper.getImageIcon("empty.gif")); menuFileCloseAll.addActionListener(this); menuFileProperties = new JMenuItem("Properties", ComponentHelper.getImageIcon("empty.gif")); menuFileProperties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK)); menuFileProperties.addActionListener(this); menuFileExit = new JMenuItem("Exit", ComponentHelper.getImageIcon("forward.gif")); menuFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.ALT_MASK)); menuFileExit.addActionListener(this); menuFile.add(menuFileOpen); menuFile.add(menuFileSave); menuFile.add(menuFileSaveAs); menuFile.add(menuFileClose); menuFile.add(menuFileCloseAll); menuFile.addSeparator(); menuFile.add(menuFileProperties); menuFile.addSeparator(); menuFile.add(menuFileExit); menuBar.add(menuFile); menuEdit = new JMenu("Edit"); menuEditUndo = new JMenuItem("Undo", ComponentHelper.getImageIcon("undo.gif")); menuEditUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK)); menuEditUndo.addActionListener(this); menuEditCopy = new JMenuItem("Copy", ComponentHelper.getImageIcon("copy.gif")); menuEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, KeyEvent.CTRL_MASK)); menuEditCopy.addActionListener(this); menuEditSearch = new JMenuItem("Search...", ComponentHelper.getImageIcon("find.gif")); menuEditSearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK)); menuEditSearch.addActionListener(this); menuEditClearSearch = new JMenuItem("Clear search", ComponentHelper.getImageIcon("empty.gif")); menuEditClearSearch.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)); menuEditClearSearch.addActionListener(this); menuEditRenameAttribute = new JMenuItem("Rename attribute", ComponentHelper.getImageIcon("empty.gif")); menuEditRenameAttribute.addActionListener(this); menuEditAttributeAsClass = new JMenuItem("Attribute as class", ComponentHelper.getImageIcon("empty.gif")); menuEditAttributeAsClass.addActionListener(this); menuEditDeleteAttribute = new JMenuItem("Delete attribute", ComponentHelper.getImageIcon("empty.gif")); menuEditDeleteAttribute.addActionListener(this); menuEditDeleteAttributes = new JMenuItem("Delete attributes", ComponentHelper.getImageIcon("empty.gif")); menuEditDeleteAttributes.addActionListener(this); menuEditDeleteInstance = new JMenuItem("Delete instance", ComponentHelper.getImageIcon("empty.gif")); menuEditDeleteInstance.addActionListener(this); menuEditDeleteInstances = new JMenuItem("Delete instances", ComponentHelper.getImageIcon("empty.gif")); menuEditDeleteInstances.addActionListener(this); menuEditSortInstances = new JMenuItem("Sort data (ascending)", ComponentHelper.getImageIcon("sort.gif")); menuEditSortInstances.addActionListener(this); menuEdit.add(menuEditUndo); menuEdit.addSeparator(); menuEdit.add(menuEditCopy); menuEdit.addSeparator(); menuEdit.add(menuEditSearch); menuEdit.add(menuEditClearSearch); menuEdit.addSeparator(); menuEdit.add(menuEditRenameAttribute); menuEdit.add(menuEditAttributeAsClass); menuEdit.add(menuEditDeleteAttribute); menuEdit.add(menuEditDeleteAttributes); menuEdit.addSeparator(); menuEdit.add(menuEditDeleteInstance); menuEdit.add(menuEditDeleteInstances); menuEdit.add(menuEditSortInstances); menuBar.add(menuEdit); menuView = new JMenu("View"); menuViewAttributes = new JMenuItem("Attributes...", ComponentHelper.getImageIcon("objects.gif")); menuViewAttributes.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)); menuViewAttributes.addActionListener(this); menuViewValues = new JMenuItem("Values...", ComponentHelper.getImageIcon("properties.gif")); menuViewValues.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)); menuViewValues.addActionListener(this); menuViewOptimalColWidths = new JMenuItem("Optimal column width (all)", ComponentHelper.getImageIcon("resize.gif")); menuViewOptimalColWidths.addActionListener(this); menuView.add(menuViewAttributes); menuView.add(menuViewValues); menuView.addSeparator(); menuView.add(menuViewOptimalColWidths); menuBar.add(menuView); // tabbed pane tabbedPane = new JTabbedPane(); tabbedPane.addChangeListener(this); add(tabbedPane, BorderLayout.CENTER); updateMenu(); updateFrameTitle(); } /** * returns the parent frame, if it's a JFrame, otherwise null * * @return the parent frame */ public JFrame getParentFrame() { if (parent instanceof JFrame) return (JFrame) parent; else return null; } /** * returns the parent frame, if it's a JInternalFrame, otherwise null * * @return the parent frame */ public JInternalFrame getParentInternalFrame() { if (parent instanceof JInternalFrame) return (JInternalFrame) parent; else return null; } /** * returns the menu bar to be added in a frame * * @return the menu bar */ public JMenuBar getMenu() { return menuBar; } /** * returns the tabbedpane instance * * @return the tabbed pane */ public JTabbedPane getTabbedPane() { return tabbedPane; } /** * whether to present a MessageBox on Exit or not * @param confirm whether a MessageBox pops up or not to confirm * exit */ public void setConfirmExit(boolean confirm) { confirmExit = confirm; } /** * returns the setting of whether to display a confirm messagebox or not * on exit * @return whether a messagebox is displayed or not */ public boolean getConfirmExit() { return confirmExit; } /** * whether to do a System.exit(0) on close * * @param value enables/disables a System.exit(0) on close */ public void setExitOnClose(boolean value) { exitOnClose = value; } /** * returns TRUE if a System.exit(0) is done on a close * * @return true if a System.exit(0) is done on close */ public boolean getExitOnClose() { return exitOnClose; } /** * validates and repaints the frame */ public void refresh() { validate(); repaint(); } /** * returns the title (incl. filename) for the frame * * @return the frame title */ public String getFrameTitle() { if (getCurrentFilename().equals("")) return frameTitle; else return frameTitle + " - " + getCurrentFilename(); } /** * sets the title of the parent frame, if one was provided */ public void updateFrameTitle() { if (getParentFrame() != null) getParentFrame().setTitle(getFrameTitle()); if (getParentInternalFrame() != null) getParentInternalFrame().setTitle(getFrameTitle()); } /** * sets the enabled/disabled state of the menu */ protected void updateMenu() { boolean fileOpen; boolean isChanged; boolean canUndo; fileOpen = (getCurrentPanel() != null); isChanged = fileOpen && (getCurrentPanel().isChanged()); canUndo = fileOpen && (getCurrentPanel().canUndo()); // File menuFileOpen.setEnabled(true); menuFileSave.setEnabled(isChanged); menuFileSaveAs.setEnabled(fileOpen); menuFileClose.setEnabled(fileOpen); menuFileCloseAll.setEnabled(fileOpen); menuFileProperties.setEnabled(fileOpen); menuFileExit.setEnabled(true); // Edit menuEditUndo.setEnabled(canUndo); menuEditCopy.setEnabled(fileOpen); menuEditSearch.setEnabled(fileOpen); menuEditClearSearch.setEnabled(fileOpen); menuEditAttributeAsClass.setEnabled(fileOpen); menuEditRenameAttribute.setEnabled(fileOpen); menuEditDeleteAttribute.setEnabled(fileOpen); menuEditDeleteAttributes.setEnabled(fileOpen); menuEditDeleteInstance.setEnabled(fileOpen); menuEditDeleteInstances.setEnabled(fileOpen); menuEditSortInstances.setEnabled(fileOpen); // View menuViewAttributes.setEnabled(fileOpen); menuViewValues.setEnabled(fileOpen); menuViewOptimalColWidths.setEnabled(fileOpen); } /** * sets the title of the tab that contains the given component * * @param component the component to set the title for */ protected void setTabTitle(JComponent component) { int index; if (!(component instanceof ArffPanel)) return; index = tabbedPane.indexOfComponent(component); if (index == -1) return; tabbedPane.setTitleAt(index, ((ArffPanel) component).getTitle()); updateFrameTitle(); } /** * returns the number of panels currently open * * @return the number of open panels */ public int getPanelCount() { return tabbedPane.getTabCount(); } /** * returns the specified panel, <code>null</code> if index is out of bounds * * @param index the index of the panel * @return the panel */ public ArffPanel getPanel(int index) { if ((index >= 0) && (index < getPanelCount())) return (ArffPanel) tabbedPane.getComponentAt(index); else return null; } /** * returns the currently selected tab index * * @return the index of the currently selected tab */ public int getCurrentIndex() { return tabbedPane.getSelectedIndex(); } /** * returns the currently selected panel * * @return the currently selected panel */ public ArffPanel getCurrentPanel() { return getPanel(getCurrentIndex()); } /** * checks whether a panel is currently selected * * @return true if a panel is currently selected */ public boolean isPanelSelected() { return (getCurrentPanel() != null); } /** * returns the filename of the specified panel * * @param index the index of the panel * @return the filename for the panel */ public String getFilename(int index) { String result; ArffPanel panel; result = ""; panel = getPanel(index); if (panel != null) result = panel.getFilename(); return result; } /** * returns the filename of the current tab * * @return the current filename */ public String getCurrentFilename() { return getFilename(getCurrentIndex()); } /** * sets the filename of the specified panel * * @param index the index of the panel * @param filename the new filename */ public void setFilename(int index, String filename) { ArffPanel panel; panel = getPanel(index); if (panel != null) { panel.setFilename(filename); setTabTitle(panel); } } /** * sets the filename of the current tab * * @param filename the new filename */ public void setCurrentFilename(String filename) { setFilename(getCurrentIndex(), filename); } /** * if the file is changed it pops up a dialog whether to change the * settings. if the project wasn't changed or saved it returns TRUE *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -