annotate java/src/uk/ac/qmul/eecs/ccmi/gui/filechooser/SpeechFileChooser.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.gui.filechooser;
f@0 21
f@0 22 import java.awt.Component;
f@0 23 import java.awt.GridBagLayout;
f@0 24 import java.awt.event.ItemEvent;
f@0 25 import java.awt.event.ItemListener;
f@0 26 import java.io.File;
f@0 27 import java.text.MessageFormat;
f@0 28 import java.util.ResourceBundle;
f@0 29
f@0 30 import javax.swing.DefaultComboBoxModel;
f@0 31 import javax.swing.JButton;
f@0 32 import javax.swing.JComboBox;
f@0 33 import javax.swing.JDialog;
f@0 34 import javax.swing.JLabel;
f@0 35 import javax.swing.JPanel;
f@0 36 import javax.swing.JScrollPane;
f@0 37 import javax.swing.filechooser.FileFilter;
f@0 38
f@0 39 import uk.ac.qmul.eecs.ccmi.gui.SpeechOptionPane;
f@0 40 import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory;
f@0 41 import uk.ac.qmul.eecs.ccmi.speech.SpeechUtilities;
f@0 42 import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;
f@0 43
f@0 44 /*
f@0 45 * An accessible file chooser. Users can browse the file system only using hearing as
f@0 46 * in the same fashion as they do when exploring a diagram.
f@0 47 *
f@0 48 */
f@0 49 @SuppressWarnings("serial")
f@0 50 class SpeechFileChooser extends JPanel implements FileChooser {
f@0 51 SpeechFileChooser(){
f@0 52 super(new GridBagLayout());
f@0 53 initComponents();
f@0 54 addComponents();
f@0 55 }
f@0 56
f@0 57 private void addComponents() {
f@0 58 GridBagUtilities gridBag = new GridBagUtilities();
f@0 59 JScrollPane scrollPane = new JScrollPane(tree);
f@0 60 add(scrollPane,gridBag.all());
f@0 61 add(fileNameLabel,gridBag.label());
f@0 62 add(fileNameTextField, gridBag.field());
f@0 63 add(fileTypeLabel, gridBag.label());
f@0 64 add(fileTypeComboBox, gridBag.field());
f@0 65 }
f@0 66
f@0 67 private void initComponents(){
f@0 68 resources = ResourceBundle.getBundle(this.getClass().getName());
f@0 69 /* file name components */
f@0 70 fileNameLabel = new JLabel(resources.getString("dialog.file_chooser.file_name"));
f@0 71 fileNameTextField = new TreeSelectionTextField();
f@0 72 fileNameTextField.setColumns(20);
f@0 73 fileNameLabel.setLabelFor(fileNameTextField);
f@0 74 fileNameTextField.addKeyListener(SpeechUtilities.getSpeechKeyListener(true));
f@0 75 fileTypeLabel = new JLabel(resources.getString("dialog.file_chooser.file_type"));
f@0 76
f@0 77 /* file type components */
f@0 78 Object [] items = {new AllFileFilter(resources.getString("all_file_filter.label"))};
f@0 79 fileTypeComboBox = new JComboBox(items);
f@0 80 fileTypeLabel.setLabelFor(fileTypeComboBox);
f@0 81
f@0 82 /* set up the listener binding the tree with the file name and file type components */
f@0 83 tree = new FileSystemTree((FileFilter)fileTypeComboBox.getSelectedItem());
f@0 84 tree.addTreeSelectionListener(fileNameTextField);
f@0 85 fileTypeComboBox.addItemListener(new ItemListener(){
f@0 86 @Override
f@0 87 public void itemStateChanged(ItemEvent evt) {
f@0 88 if(evt.getStateChange() == ItemEvent.SELECTED)
f@0 89 tree.applyFilter((FileFilter)evt.getItem());
f@0 90 }
f@0 91 });
f@0 92 }
f@0 93
f@0 94 @Override
f@0 95 public void setSelectedFile(File file){
f@0 96 selectedFile = file;
f@0 97 if(file == null)
f@0 98 return;
f@0 99 tree.setSelectionPath(file);
f@0 100 }
f@0 101
f@0 102 @Override
f@0 103 public File getSelectedFile(){
f@0 104 return selectedFile;
f@0 105 }
f@0 106
f@0 107 @Override
f@0 108 public void setFileFilter(FileFilter filter){
f@0 109 if(filter != null){
f@0 110 FileFilter wrap = new WrapFileFilter(filter);
f@0 111 ((DefaultComboBoxModel)fileTypeComboBox.getModel()).insertElementAt(wrap, 0);
f@0 112 fileTypeComboBox.getModel().setSelectedItem(wrap);
f@0 113 }
f@0 114 }
f@0 115
f@0 116 @Override
f@0 117 public void resetChoosableFileFilters(){
f@0 118 if(fileTypeComboBox.getItemCount() == 2)
f@0 119 ((DefaultComboBoxModel)fileTypeComboBox.getModel()).removeElementAt(0);
f@0 120 }
f@0 121
f@0 122 @Override
f@0 123 public void setCurrentDirectory(File dir){
f@0 124 currentDir = dir;
f@0 125 if(dir == null)
f@0 126 return;
f@0 127 tree.setSelectionPath(dir);
f@0 128 }
f@0 129
f@0 130 @Override
f@0 131 public File getCurrentDirectory(){
f@0 132 return currentDir;
f@0 133 }
f@0 134
f@0 135 @Override
f@0 136 public int showOpenDialog(Component parent){
f@0 137 FileSystemTreeNode treeNode = (FileSystemTreeNode)tree.getSelectionPath().getLastPathComponent();
f@0 138 NarratorFactory.getInstance().speak(
f@0 139 MessageFormat.format(
f@0 140 resources.getString("dialog.open.message"),
f@0 141 treeNode.spokenText()));
f@0 142 return showDialog(parent,true);
f@0 143 }
f@0 144
f@0 145 @Override
f@0 146 public int showSaveDialog(Component parent){
f@0 147 FileSystemTreeNode treeNode = (FileSystemTreeNode)tree.getSelectionPath().getLastPathComponent();
f@0 148 NarratorFactory.getInstance().speak(
f@0 149 MessageFormat.format(
f@0 150 resources.getString("dialog.save.message"),
f@0 151 treeNode.spokenText()));
f@0 152 return showDialog(parent,false);
f@0 153 }
f@0 154
f@0 155 private int showDialog(Component parent, boolean isOpenFileDialog){
f@0 156 /* overrides on close so that, before closing the dialog it checks that a file name has actually *
f@0 157 * been entered by the user. If not, the dialog won't close and a error will be notified through the narrator */
f@0 158 SpeechOptionPane optionPane = new SpeechOptionPane(resources.getString("dialog.open.title")){
f@0 159 @Override
f@0 160 protected void onClose(JDialog dialog, JButton source){
f@0 161 if(source.equals(getOkButton())){
f@0 162 if(fileNameTextField.getText().isEmpty()){
f@0 163 NarratorFactory.getInstance().speak(resources.getString("dialog.error.no_file_name"));
f@0 164 return;
f@0 165 }
f@0 166 }
f@0 167 super.onClose(dialog, source);
f@0 168 }
f@0 169 };
f@0 170
f@0 171 if(isOpenFileDialog)
f@0 172 optionPane.getOkButton().setText(resources.getString("open_button.label"));
f@0 173 else
f@0 174 optionPane.getOkButton().setText(resources.getString("save_button.label"));
f@0 175 optionPane.getCancelButton().setText(resources.getString("cancel_button.label"));
f@0 176
f@0 177 /* add the speech listener just before showing up and then remove it, otherwise it will talk when filters are added/removed */
f@0 178 fileTypeComboBox.addItemListener(SpeechUtilities.getSpeechComboBoxItemListener());
f@0 179 int result = optionPane.showDialog(parent, this);
f@0 180 fileTypeComboBox.removeItemListener(SpeechUtilities.getSpeechComboBoxItemListener());
f@0 181
f@0 182 if(result == SpeechOptionPane.OK_OPTION){
f@0 183 FileSystemTreeNode treeNode = (FileSystemTreeNode)tree.getSelectionPath().getLastPathComponent();
f@0 184 /* user has made his choice. The returned file will be the directory selected in the tree *
f@0 185 * or the parent directory of the selected file if a file is selected + the string entered in the JTextField */
f@0 186 File directory = treeNode.getFile();
f@0 187 if(!directory.isDirectory())
f@0 188 directory = directory.getParentFile();
f@0 189
f@0 190 selectedFile = new File(directory,fileNameTextField.getText());
f@0 191 return APPROVE_OPTION;
f@0 192 }else{
f@0 193 return CANCEL_OPTION;
f@0 194 }
f@0 195 }
f@0 196
f@0 197 private ResourceBundle resources;
f@0 198
f@0 199 private JLabel fileNameLabel;
f@0 200 private TreeSelectionTextField fileNameTextField;
f@0 201 private JLabel fileTypeLabel;
f@0 202 private JComboBox fileTypeComboBox;
f@0 203 private FileSystemTree tree;
f@0 204 private File selectedFile;
f@0 205 private File currentDir;
f@0 206 }
f@0 207
f@0 208 class AllFileFilter extends FileFilter {
f@0 209 AllFileFilter(String description){
f@0 210 this.description = description;
f@0 211 }
f@0 212
f@0 213 @Override
f@0 214 public boolean accept(File file) {
f@0 215 return true;
f@0 216 }
f@0 217
f@0 218 @Override
f@0 219 public String getDescription() {
f@0 220 return description;
f@0 221 }
f@0 222
f@0 223 @Override
f@0 224 public String toString(){
f@0 225 return description;
f@0 226 }
f@0 227 String description;
f@0 228 }
f@0 229
f@0 230 class WrapFileFilter extends FileFilter {
f@0 231 WrapFileFilter(FileFilter delegate){
f@0 232 this.delegate = delegate;
f@0 233 }
f@0 234
f@0 235 @Override
f@0 236 public boolean accept(File f) {
f@0 237 return delegate.accept(f);
f@0 238 }
f@0 239
f@0 240 @Override
f@0 241 public String getDescription() {
f@0 242 return delegate.getDescription();
f@0 243 }
f@0 244
f@0 245 @Override
f@0 246 public String toString(){
f@0 247 return delegate.getDescription();
f@0 248 }
f@0 249
f@0 250 FileFilter delegate;
f@0 251 }
f@0 252