View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.examples.tutorial.mainwnd;
17  
18  import java.awt.Desktop;
19  import java.io.File;
20  import java.util.List;
21  
22  import net.sf.jguiraffe.gui.builder.utils.MessageOutput;
23  import net.sf.jguiraffe.gui.cmd.CommandBase;
24  import net.sf.jguiraffe.resources.Message;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * <p>
31   * An abstract base class for action tasks that perform a desktop operation with
32   * the currently selected file.
33   * </p>
34   * <p>
35   * The tutorial application defines some actions for doing something with files
36   * which is implemented by the {@code Desktop} class new in Java 1.6 (e.g.
37   * opening a file or printing it). This abstract base class provides basic
38   * functionality for implementing such functionality. It is initialized with a
39   * reference to the main controller from which the currently selected file can
40   * be obtained. It also takes care for exception handling. A concrete subclass
41   * only has to implement the desired {@code Desktop} operation.
42   * </p>
43   *
44   * @author Oliver Heger
45   * @version $Id: AbstractDesktopTask.java 205 2012-01-29 18:29:57Z oheger $
46   */
47  public abstract class AbstractDesktopTask implements Runnable
48  {
49      /** The resource ID of the title of the error message box. */
50      private static final String RESID_ERR_TITLE = "task_errdesktop_title";
51  
52      /** The resource ID of the text of the error message box. */
53      private static final String RESID_ERR_TEXT = "task_errdesktop_msg";
54  
55      /** The logger. */
56      protected final Log log = LogFactory.getLog(getClass());
57  
58      /** A reference to the main controller. */
59      private final MainWndController controller;
60  
61      /**
62       * Creates a new instance of {@code AbstractDesktopTask} and sets the
63       * reference to the main controller.
64       *
65       * @param ctrl the main controller reference
66       */
67      protected AbstractDesktopTask(MainWndController ctrl)
68      {
69          controller = ctrl;
70      }
71  
72      /**
73       * Returns a reference to the main controller.
74       *
75       * @return the main controller
76       */
77      public MainWndController getController()
78      {
79          return controller;
80      }
81  
82      /**
83       * Returns the selected file.
84       *
85       * @return the selected file
86       */
87      public File getSelectedFile()
88      {
89          List<File> selection = controller.getSelectedFiles();
90          assert selection.size() == 1 : "Wrong number of selected files!";
91          return selection.get(0);
92      }
93  
94      /**
95       * Executes this task. This implementation creates a new command for
96       * executing the desktop operation.
97       */
98      @Override
99      public void run()
100     {
101         getController().getApplication().execute(new DesktopCommand());
102     }
103 
104     /**
105      * Performs the desired operation with the selected file. This method has to
106      * be implemented by concrete subclasses.
107      *
108      * @param desktop the {@code Desktop} instance
109      * @throws Exception if an error occurs
110      */
111     protected abstract void performDesktopOperation(Desktop desktop)
112             throws Exception;
113 
114     /**
115      * A specialized command implementation for executing the desktop operation
116      * in a background thread.
117      */
118     private class DesktopCommand extends CommandBase
119     {
120         /**
121          * Executes this command. This implementation calls the
122          * {@code performDesktopOperation()} method.
123          */
124         @Override
125         public void execute() throws Exception
126         {
127             performDesktopOperation(Desktop.getDesktop());
128         }
129 
130         /**
131          * Performs UI updates after executing the command. This implementation
132          * checks whether an error occurred. If this is the case, an exception
133          * message is displayed to the user.
134          */
135         @Override
136         protected void performGUIUpdate()
137         {
138             if (getException() != null)
139             {
140                 log.error("Error on Desktop operation", getException());
141                 getController()
142                         .getApplication()
143                         .getApplicationContext()
144                         .messageBox(
145                                 new Message(null, RESID_ERR_TEXT,
146                                         getSelectedFile().getName()),
147                                 RESID_ERR_TITLE, MessageOutput.MESSAGE_ERROR,
148                                 MessageOutput.BTN_OK);
149             }
150         }
151     }
152 }