📄 arffviewermainpanel.java
字号:
if (!isPanelSelected())
return true;
result = !getCurrentPanel().isChanged();
if (getCurrentPanel().isChanged()) {
try {
if (showCancel)
button = ComponentHelper.showMessageBox(
this,
"Changed",
"The file is not saved - Do you want to save it?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE );
else
button = ComponentHelper.showMessageBox(
this,
"Changed",
"The file is not saved - Do you want to save it?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE );
}
catch (Exception e) {
button = JOptionPane.CANCEL_OPTION;
}
switch (button) {
case JOptionPane.YES_OPTION:
saveFile();
result = !getCurrentPanel().isChanged();
break;
case JOptionPane.NO_OPTION:
result = true;
break;
case JOptionPane.CANCEL_OPTION:
result = false;
break;
}
}
return result;
}
/**
* loads the specified file
*/
public void loadFile(String filename) {
ArffPanel panel;
panel = new ArffPanel(filename);
panel.addChangeListener(this);
tabbedPane.addTab(panel.getTitle(), panel);
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
}
/**
* loads the specified file into the table
*/
public void loadFile() {
int retVal;
int i;
String filename;
retVal = fileChooser.showOpenDialog(this);
if (retVal != FileChooser.APPROVE_OPTION)
return;
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
for (i = 0; i< fileChooser.getSelectedFiles().length; i++) {
filename = fileChooser.getSelectedFiles()[i].getAbsolutePath();
loadFile(filename);
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
/**
* saves the current data into a file
*/
public void saveFile() {
ArffPanel panel;
String filename;
AbstractSaver saver;
// no panel? -> exit
panel = getCurrentPanel();
if (panel == null)
return;
filename = panel.getFilename();
if (filename.equals(ArffPanel.TAB_INSTANCES)) {
saveFileAs();
}
else {
if (fileChooser.getFileFilter() == arffFilter)
saver = new ArffSaver();
else if (fileChooser.getFileFilter() == csvFilter)
saver = new CSVSaver();
else
saver = null;
if (saver != null) {
try {
saver.setRetrieval(AbstractSaver.BATCH);
saver.setInstances(panel.getInstances());
saver.setFile(fileChooser.getSelectedFile());
saver.setDestination(fileChooser.getSelectedFile());
saver.writeBatch();
panel.setChanged(false);
setCurrentFilename(filename);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* saves the current data into a new file
*/
public void saveFileAs() {
int retVal;
ArffPanel panel;
// no panel? -> exit
panel = getCurrentPanel();
if (panel == null) {
System.out.println("nothing selected!");
return;
}
if (!getCurrentFilename().equals("")) {
try {
fileChooser.setSelectedFile(new File(getCurrentFilename()));
}
catch (Exception e) {
// ignore
}
}
retVal = fileChooser.showSaveDialog(this);
if (retVal != FileChooser.APPROVE_OPTION)
return;
panel.setChanged(false);
setCurrentFilename(fileChooser.getSelectedFile().getAbsolutePath());
saveFile();
}
/**
* closes the current tab
*/
public void closeFile() {
closeFile(true);
}
/**
* closes the current tab
* @param showCancel whether to show an additional CANCEL button
* in the "Want to save changes"-dialog
* @see #saveChanges(boolean)
*/
public void closeFile(boolean showCancel) {
if (getCurrentIndex() == -1)
return;
if (!saveChanges(showCancel))
return;
tabbedPane.removeTabAt(getCurrentIndex());
updateFrameTitle();
System.gc();
}
/**
* closes all open files
*/
public void closeAllFiles() {
while (tabbedPane.getTabCount() > 0) {
if (!saveChanges(true))
return;
tabbedPane.removeTabAt(getCurrentIndex());
updateFrameTitle();
System.gc();
}
}
/**
* displays some properties of the instances
*/
public void showProperties() {
ArffPanel panel;
ListSelectorDialog dialog;
Vector props;
Instances inst;
panel = getCurrentPanel();
if (panel == null)
return;
inst = panel.getInstances();
if (inst == null)
return;
if (inst.classIndex() < 0)
inst.setClassIndex(inst.numAttributes() - 1);
// get some data
props = new Vector();
props.add("Filename: " + panel.getFilename());
props.add("Relation name: " + inst.relationName());
props.add("# of instances: " + inst.numInstances());
props.add("# of attributes: " + inst.numAttributes());
props.add("Class attribute: " + inst.classAttribute().name());
props.add("# of class labels: " + inst.numClasses());
dialog = new ListSelectorDialog(parent, new JList(props));
dialog.showDialog();
}
/**
* closes the window, i.e., if the parent is not null and implements
* the WindowListener interface it calls the windowClosing method
*/
public void close() {
if ( (parent != null) && (parent instanceof WindowListener) )
((WindowListener) parent).windowClosing(null);
}
/**
* undoes the last action
*/
public void undo() {
if (!isPanelSelected())
return;
getCurrentPanel().undo();
}
/**
* copies the content of the selection to the clipboard
*/
public void copyContent() {
if (!isPanelSelected())
return;
getCurrentPanel().copyContent();
}
/**
* searches for a string in the cells
*/
public void search() {
if (!isPanelSelected())
return;
getCurrentPanel().search();
}
/**
* clears the search, i.e. resets the found cells
*/
public void clearSearch() {
if (!isPanelSelected())
return;
getCurrentPanel().clearSearch();
}
/**
* renames the current selected Attribute
*/
public void renameAttribute() {
if (!isPanelSelected())
return;
getCurrentPanel().renameAttribute();
}
/**
* deletes the current selected Attribute or several chosen ones
*/
public void deleteAttribute(boolean multiple) {
if (!isPanelSelected())
return;
if (multiple)
getCurrentPanel().deleteAttributes();
else
getCurrentPanel().deleteAttribute();
}
/**
* deletes the current selected Instance or several chosen ones
*/
public void deleteInstance(boolean multiple) {
if (!isPanelSelected())
return;
if (multiple)
getCurrentPanel().deleteInstances();
else
getCurrentPanel().deleteInstance();
}
/**
* sorts the current selected attribute
*/
public void sortInstances() {
if (!isPanelSelected())
return;
getCurrentPanel().sortInstances();
}
/**
* displays all the attributes, returns the selected item or NULL if canceled
*/
public String showAttributes() {
ArffTableSorter model;
ListSelectorDialog dialog;
int i;
JList list;
String name;
int result;
if (!isPanelSelected())
return null;
list = new JList(getCurrentPanel().getAttributes());
dialog = new ListSelectorDialog(parent, list);
result = dialog.showDialog();
if (result == ListSelectorDialog.APPROVE_OPTION) {
model = (ArffTableSorter) getCurrentPanel().getTable().getModel();
name = list.getSelectedValue().toString();
i = model.getAttributeColumn(name);
JTableHelper.scrollToVisible(getCurrentPanel().getTable(), 0, i);
getCurrentPanel().getTable().setSelectedColumn(i);
return name;
}
else {
return null;
}
}
/**
* displays all the distinct values for an attribute
*/
public void showValues() {
String attribute;
ArffTableSorter model;
ArffTable table;
HashSet values;
Vector items;
Iterator iter;
ListSelectorDialog dialog;
int i;
int col;
// choose attribute to retrieve values for
attribute = showAttributes();
if (attribute == null)
return;
table = (ArffTable) getCurrentPanel().getTable();
model = (ArffTableSorter) table.getModel();
// get column index
col = -1;
for (i = 0; i < table.getColumnCount(); i++) {
if (table.getPlainColumnName(i).equals(attribute)) {
col = i;
break;
}
}
// not found?
if (col == -1)
return;
// get values
values = new HashSet();
items = new Vector();
for (i = 0; i < model.getRowCount(); i++)
values.add(model.getValueAt(i, col).toString());
if (values.isEmpty())
return;
iter = values.iterator();
while (iter.hasNext())
items.add(iter.next());
Collections.sort(items);
dialog = new ListSelectorDialog(parent, new JList(items));
dialog.showDialog();
}
/**
* invoked when an action occurs
*/
public void actionPerformed(ActionEvent e) {
Object o;
o = e.getSource();
if (o == menuFileOpen)
loadFile();
else if (o == menuFileSave)
saveFile();
else if (o == menuFileSaveAs)
saveFileAs();
else if (o == menuFileClose)
closeFile();
else if (o == menuFileCloseAll)
closeAllFiles();
else if (o == menuFileProperties)
showProperties();
else if (o == menuFileExit)
close();
else if (o == menuEditUndo)
undo();
else if (o == menuEditCopy)
copyContent();
else if (o == menuEditSearch)
search();
else if (o == menuEditClearSearch)
clearSearch();
else if (o == menuEditDeleteAttribute)
deleteAttribute(false);
else if (o == menuEditDeleteAttributes)
deleteAttribute(true);
else if (o == menuEditRenameAttribute)
renameAttribute();
else if (o == menuEditDeleteInstance)
deleteInstance(false);
else if (o == menuEditDeleteInstances)
deleteInstance(true);
else if (o == menuEditSortInstances)
sortInstances();
else if (o == menuViewAttributes)
showAttributes();
else if (o == menuViewValues)
showValues();
updateMenu();
}
/**
* Invoked when the target of the listener has changed its state.
*/
public void stateChanged(ChangeEvent e) {
updateFrameTitle();
updateMenu();
// did the content of panel change? -> change title of tab
if (e.getSource() instanceof JComponent)
setTabTitle((JComponent) e.getSource());
}
/**
* returns only the classname
*/
public String toString() {
return this.getClass().getName();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -