001    package nl.cwi.sen1.gui.plugin;
002    
003    import java.io.File;
004    
005    import javax.swing.JFileChooser;
006    import javax.swing.JOptionPane;
007    import javax.swing.SwingUtilities;
008    import javax.swing.filechooser.FileFilter;
009    
010    import nl.cwi.sen1.configapi.Factory;
011    import nl.cwi.sen1.gui.Studio;
012    import nl.cwi.sen1.gui.StudioImpl;
013    import aterm.ATerm;
014    import aterm.ATermList;
015    
016    public class Dialog extends DefaultStudioPlugin implements DialogTif {
017        private static final String WORKING_DIRECTORY = "user.dir";
018    
019        private static final String TOOL_NAME = "dialog";
020    
021        private Studio studio;
022    
023        private Factory factory;
024    
025        private ProgressList progressList;
026    
027        private JFileChooser sharedChooser;
028        private FileFilter sharedFilter;
029    
030        public Dialog() {
031            sharedChooser = new JFileChooser(System.getProperty(WORKING_DIRECTORY));
032        }
033    
034        public String getName() {
035            return TOOL_NAME;
036        }
037    
038        public void initStudioPlugin(Studio studio) {
039            this.studio = studio;
040            DialogBridge bridge = new DialogBridge(studio.getATermFactory(), this);
041            factory = Factory.getInstance((aterm.pure.PureFactory) studio
042                    .getATermFactory());
043            studio.connect(getName(), bridge);
044        }
045    
046        public ATerm showDirectoryDialog(String title, ATerm paths) {
047            JFileChooser chooser = sharedChooser;
048    
049            if (!((ATermList) paths).isEmpty()) {
050                DialogFileSystemView fsv = new DialogFileSystemView(factory
051                        .PropertyListFromTerm(paths));
052                chooser = new JFileChooser(fsv);
053                chooser.setFileView(new DialogFileView(fsv));
054            }
055            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
056    
057            chooser.setSelectedFile(new File(""));
058            if (chooser.showDialog(StudioImpl.getFrame(), title) == JFileChooser.APPROVE_OPTION) {
059                String path = chooser.getSelectedFile().getAbsolutePath();
060                return studio.getATermFactory().make(
061                        "snd-value(directory-dialog-approve(<str>))", path);
062            }
063            return studio.getATermFactory().make(
064                    "snd-value(directory-dialog-cancel)");
065        }
066    
067        public ATerm showFileDialog(String title, ATerm paths,
068                final String extension) {
069            JFileChooser chooser = sharedChooser;
070    
071            if (!((ATermList) paths).isEmpty()) {
072                DialogFileSystemView fsv = new DialogFileSystemView(factory
073                        .PropertyListFromTerm(paths));
074                chooser = new JFileChooser(fsv);
075                chooser.setFileView(new DialogFileView(fsv));
076            }
077    
078            chooser.removeChoosableFileFilter(sharedFilter);
079            sharedFilter = new FileFilter() {
080                public boolean accept(File file) {
081                    if (file.isDirectory()) {
082                        return true;
083                    }
084    
085                    String filename = file.getName();
086                    return filename.endsWith(extension);
087                }
088    
089                public String getDescription() {
090                    return "*" + extension;
091                }
092            };
093    
094            chooser.setSelectedFile(new File(""));
095            chooser.addChoosableFileFilter(sharedFilter);
096            
097            if (chooser.showDialog(StudioImpl.getFrame(), title) == JFileChooser.APPROVE_OPTION) {
098                File file = chooser.getSelectedFile();
099                
100                String path = buildSelectedFile(file, extension);
101                return studio.getATermFactory().make(
102                        "snd-value(file-dialog-approve(<str>))", path);
103            }
104            return studio.getATermFactory().make("snd-value(file-dialog-cancel)");
105        }
106        
107        private String buildSelectedFile(File file, String extension) {
108            String path = file.getAbsolutePath();
109            String last = file.getName();
110            
111            if (last.lastIndexOf('.') == -1) { 
112                    /* no extension */
113                    path = path.concat(extension.startsWith(".") ? extension : ("." + extension));
114            }
115            else if (last.endsWith(".")) {
116                    /* file ends in . */
117                    path = path.concat(extension.startsWith(".") ? extension.substring(1) : extension);
118            }
119            
120            return path;
121        }
122    
123        public void recTerminate(ATerm t0) {
124            fireStudioPluginClosed();
125        }
126    
127        public void showProgressMessage(String message) {
128            if (progressList != null) {
129                progressList.addMessage(message);
130            }
131        }
132    
133        public void showProgressList(final String title) {
134            progressList = new ProgressList(StudioImpl.getFrame(), title, true);
135            SwingUtilities.invokeLater(new Runnable() {
136                public void run() {
137                    progressList.setVisible(true);
138                }
139            });
140        }
141    
142        public void closeProgressList() {
143            if (progressList != null) {
144                progressList.dispose();
145            }
146        }
147    
148        public void showProgressMessageWithArguments(String format, ATerm args) {
149            String message = StringFormatter.format(format, (ATermList) args);
150            if (progressList != null) {
151                progressList.addMessage(message);
152            }
153        }
154    
155        public ATerm showQuestionDialog(String question) {
156            int choice = JOptionPane.showConfirmDialog(StudioImpl.getFrame(), question);
157    
158            if (choice == JOptionPane.YES_OPTION) {
159                return studio.getATermFactory().make("snd-value(answer(yes))");
160            }
161            if (choice == JOptionPane.NO_OPTION) {
162                return studio.getATermFactory().make("snd-value(answer(no))");
163            }
164    
165            return studio.getATermFactory().make("snd-value(answer(cancel))");
166        }
167    
168        public void showMessageDialog(String message) {
169            JOptionPane.showMessageDialog(StudioImpl.getFrame(), message, "Info",
170                    JOptionPane.INFORMATION_MESSAGE);
171        }
172    
173        public void showErrorDialog(String errorMessage) {
174            if (progressList != null) {
175                progressList.dispose();
176            }
177            JOptionPane.showMessageDialog(StudioImpl.getFrame(), errorMessage, "Error",
178                    JOptionPane.ERROR_MESSAGE);
179        }
180    
181        public void showErrorDialogWithArguments(String errorMessage, ATerm args) {
182            String message = StringFormatter.format(errorMessage, (ATermList) args);
183            if (progressList != null) {
184                progressList.dispose();
185            }
186            JOptionPane.showMessageDialog(StudioImpl.getFrame(), message, "Error",
187                    JOptionPane.ERROR_MESSAGE);
188        }
189    }