View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.file.action;
14  
15  import java.awt.Component;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.KeyEvent;
18  import java.io.File;
19  import java.io.IOException;
20  
21  import javax.swing.Action;
22  import javax.swing.JFileChooser;
23  import javax.swing.KeyStroke;
24  
25  import org.abstracthorizon.aequo.file.FilesModel;
26  
27  /**
28   * Delete files from selected side action.
29   *
30   * @author Daniel Sendula
31   */
32  public class ReplaceSideAction extends FileBaseAction {
33  
34      public ReplaceSideAction() {
35          setName("Open");
36          setMessage("Open");
37          setDescription("Open");
38          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
39          putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
40      }
41  
42      public boolean isEnabled() {
43          return true;
44      }
45      
46      /**
47       * Replaces a top file of a side with newly selected one
48       * 
49       * @param e event 
50       */
51      public void perform(ActionEvent e) {
52          FilesModel filesModel = getFilesModel();
53          File path = filesModel.getData(column);
54  
55          JFileChooser fileChooser;
56          if (path != null) {
57              fileChooser = new JFileChooser(path);
58          } else {
59              fileChooser = new JFileChooser();
60          }
61          fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
62          
63          int res = fileChooser.showOpenDialog((Component)e.getSource());
64          if (res == JFileChooser.APPROVE_OPTION) {
65              File file = fileChooser.getSelectedFile();
66              path = file.getParentFile();
67              context.getPreferences().setProperty("path", path.getAbsolutePath());
68              try {
69                  context.getPreferences().save();
70              } catch (IOException ignore) {
71              }
72  
73              filesModel.setData(column, file);
74              filesModel.refresh();
75          }
76      }
77  }