diff --git a/unitex/src/fr/umlv/unitex/frames/GraphPathFrame.java b/unitex/src/fr/umlv/unitex/frames/GraphPathFrame.java index ddc2251ec..f1603632c 100644 --- a/unitex/src/fr/umlv/unitex/frames/GraphPathFrame.java +++ b/unitex/src/fr/umlv/unitex/frames/GraphPathFrame.java @@ -21,6 +21,7 @@ package fr.umlv.unitex.frames; +import fr.umlv.unitex.common.project.manager.GlobalProjectManager; import fr.umlv.unitex.config.Config; import fr.umlv.unitex.config.ConfigManager; import fr.umlv.unitex.files.FileUtil; @@ -37,16 +38,25 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import javax.swing.DefaultComboBoxModel; +import javax.swing.DefaultListCellRenderer; +import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JInternalFrame; +import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; -public class GraphPathFrame extends JInternalFrame { +public class GraphPathFrame extends JInternalFrame implements + MultiInstanceFrameFactoryObserver { + private List graphFrames; + private GraphFrame currentFrame; MultiCommands preprocessCommands; Boolean flattenMode = false; String flattenDepth = "10"; @@ -95,9 +105,37 @@ public void contentsChanged(ListDataEvent e) { * Creates new form GPF */ public GraphPathFrame() { + currentFrame = GlobalProjectManager.search(null) + .getFrameManagerAs(InternalFrameManager.class) + .getCurrentFocusedGraphFrame(); + graphFrames = GlobalProjectManager.search(null) + .getFrameManagerAs(InternalFrameManager.class) + .getGraphFrames(); + setGraphPathFrame(); + } + + private void setGraphPathFrame() { + if (graphFrames.isEmpty()) { + throw new AssertionError("graphFrames should not be empty in construction of GraphPathFrame"); + } + if (currentFrame == null) { + currentFrame = graphFrames.get(0); + } initComponents(); + fillComboBox(); + setOutputFileDefaultName(currentFrame); } + /** + * This method return an empty string if the selected graph is null or unsaved, otherwise the selected graph name + * */ + private String getSelectedGraphName() { + GraphFrame graphFrame = (GraphFrame) inputGraphName.getSelectedItem(); + if (graphFrame == null || graphFrame.getGraph() == null) { + return ""; + } + return FileUtil.getFileNameWithoutExtension(graphFrame.getGraph()); + } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -111,7 +149,7 @@ private void initComponents() { buttonGroup2 = new javax.swing.ButtonGroup(); graphFileLabel = new javax.swing.JLabel(); outputFileLabel = new javax.swing.JLabel(); - inputGraphName = new javax.swing.JTextField(); + inputGraphName = new javax.swing.JComboBox(); outputFileName = new javax.swing.JTextField(); setFileButton = new javax.swing.JButton(); optionSeparator = new javax.swing.JSeparator(); @@ -150,7 +188,6 @@ private void initComponents() { outputFileLabel.setText("Output file:"); inputGraphName.setEditable(false); - inputGraphName.setText("jTextField1"); inputGraphName.setPreferredSize(new java.awt.Dimension(70, 25)); inputGraphName.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { @@ -402,62 +439,87 @@ private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- }//GEN-LAST:event_cancelButtonActionPerformed private void exploreRecButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exploreRecButtonActionPerformed + String selectedGraphName = getSelectedGraphName(); + if (selectedGraphName.isEmpty()) { + outputFileName.setText(""); + return; + } if(!makeDicCheckBox.isSelected()) { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-recursive-paths.txt"); + outputFileName.setText(selectedGraphName + "-recursive-paths.txt"); } else { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-recursive-paths.dic"); + outputFileName.setText(selectedGraphName + "-recursive-paths.dic"); } }//GEN-LAST:event_exploreRecButtonActionPerformed private void inputGraphNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputGraphNameActionPerformed - // TODO add your handling code here: + GraphFrame f = (GraphFrame) inputGraphName.getSelectedItem(); + setOutputFileDefaultName(f); }//GEN-LAST:event_inputGraphNameActionPerformed private void exploreIndepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exploreIndepButtonActionPerformed + String selectedGraphName = getSelectedGraphName(); + if (selectedGraphName.isEmpty()) { + outputFileName.setText(""); + return; + } if(!makeDicCheckBox.isSelected()) { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-paths.txt"); + outputFileName.setText(selectedGraphName + "-paths.txt"); } else { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-paths.dic"); + outputFileName.setText(selectedGraphName + "-paths.dic"); } }//GEN-LAST:event_exploreIndepButtonActionPerformed private void makeDicCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputGraphNameActionPerformed + String selectedGraphName = getSelectedGraphName(); if(makeDicCheckBox.isSelected()) { separateOutputsButton.setEnabled(false); alternateOutputsButton.setEnabled(false); ignoreOutputsButton.setEnabled(false); separateOutputsButton.setSelected(true); + if (selectedGraphName.isEmpty()) { + outputFileName.setText(""); + return; + } if(exploreRecButton.isSelected()) { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-recursive-paths.dic"); + outputFileName.setText(selectedGraphName + "-recursive-paths.dic"); } else { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-paths.dic"); + outputFileName.setText(selectedGraphName+ "-paths.dic"); } } else { separateOutputsButton.setEnabled(true); alternateOutputsButton.setEnabled(true); ignoreOutputsButton.setEnabled(true); + if (selectedGraphName.isEmpty()) { + outputFileName.setText(""); + return; + } if(exploreRecButton.isSelected()) { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-recursive-paths.txt"); + outputFileName.setText(selectedGraphName + "-recursive-paths.txt"); } else { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-paths.txt"); + outputFileName.setText(selectedGraphName + "-paths.txt"); } } }//GEN-LAST:event_inputGraphNameActionPerformed private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed + if (inputGraphName.getSelectedItem() == null || + ((GraphFrame) inputGraphName.getSelectedItem()).getGraph() == null) { + JOptionPane.showMessageDialog(UnitexFrame.mainFrame, + "Cannot explore graph paths for graph with no name, save the graph first", "Error", + JOptionPane.ERROR_MESSAGE); + return; + } + if (outputFileName.getText().isEmpty()) { + JOptionPane.showMessageDialog(UnitexFrame.mainFrame, + "Cannot explore graph paths with empty file output", "Error", + JOptionPane.ERROR_MESSAGE); + return; + } Fst2ListCommand cmd = new Fst2ListCommand(); final Grf2Fst2Command grfCmd = new Grf2Fst2Command(); File fst2; @@ -469,7 +531,7 @@ private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR maxSeqSpinner.commitEdit(); n = (Integer) maxSeqSpinner.getValue(); } catch (final NumberFormatException | ParseException e) { - JOptionPane.showMessageDialog(null, + JOptionPane.showMessageDialog(UnitexFrame.mainFrame, "You must specify a valid limit", "Error", JOptionPane.ERROR_MESSAGE); return; @@ -494,14 +556,15 @@ private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR if ( !checkLoopsCheckbox.isSelected() ) { cmd = cmd.noLoopCheck(); } + String selectedGraphName = getSelectedGraphName(); // check if flatten was checked or not if( !flattenCheckbox.isSelected() ) { - grfCmd.grf(new File(inputGraphName.getText())) + grfCmd.grf(new File(selectedGraphName)) .enableLoopAndRecursionDetection(true).repositories() .emitEmptyGraphWarning().displayGraphNames(); } else if ( preprocessCommands == null ) { // if no specific option were given, preprocess with default - File graphFile = new File(inputGraphName.getText()); + File graphFile = new File(selectedGraphName); String name_fst2 = FileUtil.getFileNameWithoutExtension(graphFile); name_fst2 = name_fst2 + ".fst2"; final MultiCommands commands = new MultiCommands(); @@ -516,8 +579,7 @@ private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR Launcher.exec(preprocessCommands, false); } - fst2 = new File(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + ".fst2"); + fst2 = new File(FileUtil.getFileNameWithoutExtension(selectedGraphName) + ".fst2"); if (exploreRecButton.isSelected()) { // set file to user input list = new File(outputFileName.getText()); @@ -527,9 +589,7 @@ private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR // if we change it here ShowPathsDo will throw a FileNotFoundException // we will rename the file once the UnitexToolLogger process has completed // alternatively that process could be changed to remove the hard coding - list = new File( - FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "autolst.txt"); + list = new File(selectedGraphName + "autolst.txt"); cmd = cmd.listsOfSubgraph(fst2); } final MultiCommands commands = new MultiCommands(); @@ -542,7 +602,7 @@ private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR }//GEN-LAST:event_runButtonActionPerformed private void flattenOptionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_flattenOptionButtonActionPerformed - File graphFile = new File(inputGraphName.getText()); + File graphFile = new File(inputGraphName.getSelectedItem().toString()); Map flattenOptions = UnitexFrame .flattenGraph(graphFile,flattenMode,flattenDepth); if( flattenOptions != null ) { @@ -605,6 +665,59 @@ void close() { outputArea.getModel().removeListDataListener(listListener); } + @Override + public void onUpdate(ArrayList frames) { + if (frames == null || frames.isEmpty()) { + dispose(); + return; + } + graphFrames = frames; + currentFrame = GlobalProjectManager.search(null).getFrameManagerAs(InternalFrameManager.class) + .getCurrentFocusedGraphFrame(); + if (currentFrame == null || currentFrame.getGraph() == null) { + currentFrame = graphFrames.get(0); + } + fillComboBox(); + setOutputFileDefaultName(currentFrame); + } + + private void fillComboBox() { + ComboBoxToolTipRenderer renderer = new ComboBoxToolTipRenderer(); + ArrayList tooltips = new ArrayList(); + DefaultComboBoxModel model = new DefaultComboBoxModel(); + for (GraphFrame f : graphFrames) { + model.addElement(f); + if (f.getGraph() == null) { + tooltips.add(f.toString()); + } + else { + tooltips.add(f.getGraph().getPath()); + } + } + renderer.setTooltips(tooltips); + inputGraphName.setRenderer(renderer); + inputGraphName.setModel(model); + } + + public class ComboBoxToolTipRenderer extends DefaultListCellRenderer { + + private ArrayList tooltips; + @Override + public JComponent getListCellRendererComponent(JList list, Object value, + int index, boolean isSelected, boolean cellHasFocus) { + JComponent comp = (JComponent) super.getListCellRendererComponent(list, + value, index, isSelected, cellHasFocus); + + if (-1 < index && null != value && null != tooltips) { + list.setToolTipText(tooltips.get(index)); + } + return comp; + } + public void setTooltips(ArrayList tooltips) { + this.tooltips = tooltips; + } + } + class ShowPathsDo implements ToDo { private final File name; @@ -634,18 +747,20 @@ public void toDo(boolean success) { } void setInputGraphName(String string) { - inputGraphName.setText(string); } - public void setOutputFileDefaultName(String graphFileName) { + private void setOutputFileDefaultName(GraphFrame graphFrame) { + if (graphFrame == null || graphFrame.getGraph() == null) { + outputFileName.setText(""); + return; + } + String graphName = FileUtil.getFileNameWithoutExtension(graphFrame.getGraph().getPath()); String extension = makeDicCheckBox.isSelected() ? ".dic" : ".txt"; if(exploreRecButton.isSelected()) { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-recursive-paths" + extension); + outputFileName.setText(graphName + "-recursive-paths" + extension); } else { - outputFileName.setText(FileUtil.getFileNameWithoutExtension(inputGraphName - .getText()) + "-paths" + extension); + outputFileName.setText(graphName + "-paths" + extension); } } @@ -679,7 +794,7 @@ private void openOutputFile() { private javax.swing.JLabel graphFileLabel; private javax.swing.JButton helpButton; private javax.swing.JRadioButton ignoreOutputsButton; - private javax.swing.JTextField inputGraphName; + private javax.swing.JComboBox inputGraphName; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JCheckBox maxSeqCheckbox; private javax.swing.JSpinner maxSeqSpinner; diff --git a/unitex/src/fr/umlv/unitex/frames/InternalFrameManager.java b/unitex/src/fr/umlv/unitex/frames/InternalFrameManager.java index 4e355f162..b20c61620 100644 --- a/unitex/src/fr/umlv/unitex/frames/InternalFrameManager.java +++ b/unitex/src/fr/umlv/unitex/frames/InternalFrameManager.java @@ -20,6 +20,7 @@ */ package fr.umlv.unitex.frames; +import fr.umlv.unitex.common.project.manager.GlobalProjectManager; import java.beans.PropertyVetoException; import java.io.File; import java.util.ArrayList; @@ -656,18 +657,12 @@ public void closeConstructSeqTfstFrame() { } public GraphPathFrame newGraphPathFrame() { - final GraphFrame gf = getCurrentFocusedGraphFrame(); - if (gf == null) { - return null; - } final GraphPathFrame d = (GraphPathFrame) setup(graphPathFrameFactory .newFrame(),true); if (d == null) return null; - final File f = gf.getGraph(); - d.setInputGraphName(f.getAbsolutePath()); - d.setOutputFileDefaultName(f.getAbsolutePath()); d.setVisible(true); + GlobalProjectManager.search(null).getFrameManagerAs(InternalFrameManager.class).addObserver(d); return d; } diff --git a/unitex/src/fr/umlv/unitex/frames/UnitexFrame.java b/unitex/src/fr/umlv/unitex/frames/UnitexFrame.java index 45abc7bca..a3f294968 100644 --- a/unitex/src/fr/umlv/unitex/frames/UnitexFrame.java +++ b/unitex/src/fr/umlv/unitex/frames/UnitexFrame.java @@ -1139,22 +1139,8 @@ public void actionPerformed(ActionEvent e) { explorePaths.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - final GraphFrame f = GlobalProjectManager.search(null) - .getFrameManagerAs(InternalFrameManager.class) - .getCurrentFocusedGraphFrame(); - if (f != null) { - - if (f.getGraph() == null) { - JOptionPane.showMessageDialog(null, - "Cannot explore graph paths for graph with no name", "Error", - JOptionPane.ERROR_MESSAGE); - return; - } - else { - GlobalProjectManager.search(null) - .getFrameManagerAs(InternalFrameManager.class).newGraphPathFrame(); - } - } + GlobalProjectManager.search(null) + .getFrameManagerAs(InternalFrameManager.class).newGraphPathFrame(); } }); @@ -1214,6 +1200,25 @@ public void actionPerformed(ActionEvent e) { tools.add(graphCollection); tools.addSeparator(); tools.add(svn); + tools.addMenuListener(new MenuAdapter() { + @Override + public void menuSelected(MenuEvent e) { + final GraphFrame f = GlobalProjectManager.search(null) + .getFrameManagerAs(InternalFrameManager.class) + .getCurrentFocusedGraphFrame(); + boolean existsFocusedGrFrame = f != null; + boolean existsAnyGrFrame = GlobalProjectManager.search(null) + .getFrameManagerAs(InternalFrameManager.class) + .getGraphFrames().size() != 0; + sortNodeLabel.setEnabled(existsFocusedGrFrame); + explorePaths.setEnabled(existsAnyGrFrame); + verifyBraces.setEnabled(existsFocusedGrFrame); + compileFST.setEnabled(existsFocusedGrFrame); + flatten.setEnabled(existsFocusedGrFrame); + graphCollection.setEnabled(existsFocusedGrFrame); + svn.setEnabled(existsFocusedGrFrame); + } + }); graphMenu.add(tools); final JMenu format = new JMenu("Format"); final JMenuItem alignment = new JMenuItem("Alignment..."); @@ -1484,7 +1489,7 @@ public void menuSelected(MenuEvent e) { printAll.setEnabled(existsAnyGrFrame); undo.setEnabled(existsFocusedGrFrame); redo.setEnabled(existsFocusedGrFrame); - tools.setEnabled(existsFocusedGrFrame); + tools.setEnabled(existsAnyGrFrame); format.setEnabled(existsFocusedGrFrame); zoom.setEnabled(existsFocusedGrFrame); findAndReplace.setEnabled(existsAnyGrFrame);