View Javadoc
1   package info.mikethomas.fahview.v6project;
2   
3   /*
4    * #%L
5    * This file is part of FAHView-v6project.
6    * %%
7    * Copyright (C) 2011 - 2017 Mike Thomas <mikepthomas@outlook.com>
8    * %%
9    * FAHView is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * %
14   * FAHView is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * %
19   * You should have received a copy of the GNU General Public License
20   * along with FAHView.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import java.awt.Image;
25  import java.beans.PropertyChangeListener;
26  import javax.swing.Action;
27  import javax.swing.Icon;
28  import javax.swing.ImageIcon;
29  import org.netbeans.api.annotations.common.StaticResource;
30  import org.netbeans.api.project.Project;
31  import org.netbeans.api.project.ProjectInformation;
32  import org.netbeans.spi.project.ProjectState;
33  import org.netbeans.spi.project.ui.LogicalViewProvider;
34  import org.netbeans.spi.project.ui.support.CommonProjectActions;
35  import org.netbeans.spi.project.ui.support.NodeFactorySupport;
36  import org.openide.filesystems.FileObject;
37  import org.openide.loaders.DataFolder;
38  import org.openide.loaders.DataObjectNotFoundException;
39  import org.openide.nodes.AbstractNode;
40  import org.openide.nodes.Children;
41  import org.openide.nodes.FilterNode;
42  import org.openide.nodes.Node;
43  import org.openide.util.Exceptions;
44  import org.openide.util.ImageUtilities;
45  import org.openide.util.Lookup;
46  import org.openide.util.NbBundle;
47  import org.openide.util.lookup.Lookups;
48  import org.openide.util.lookup.ProxyLookup;
49  
50  /**
51   * <p>WorkProject class.</p>
52   *
53   * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
54   * @version $Id: $Id
55   */
56  public class WorkProject implements Project {
57  
58      /** Constant <code>PROJECT_ICON</code>. */
59      @StaticResource()
60      public static final String PROJECT_ICON =
61              "info/mikethomas/fahview/v6project/work.png";
62      /** Constant <code>PROJECT_FILE</code>. */
63      public static final String PROJECT_FILE = "current.xyz";
64  
65      /** FileObject <code>directory</code>. */
66      private final FileObject directory;
67      /** ProjectState <code>state</code>. */
68      private final ProjectState state;
69      /** Lookup <code>lookup</code>. */
70      private Lookup lookup;
71  
72      /**
73       * <p>Constructor for WorkProject.</p>
74       *
75       * @param projectDirectory FileObject
76       * @param projectState ProjectState
77       */
78      WorkProject(final FileObject projectDirectory,
79                  final ProjectState projectState) {
80          this.directory = projectDirectory;
81          this.state = projectState;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final FileObject getProjectDirectory() {
87          return directory;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public final Lookup getLookup() {
93          if (lookup == null) {
94              lookup = Lookups.fixed(new Object[]{
95                  this,
96                  new WorkProjectInformation(),
97                  new WorkProjectLogicalView(this)
98              });
99          }
100         return lookup;
101     }
102 
103     /**
104      * <p>WorkProjectInformation class.</p>
105      */
106     private final class WorkProjectInformation implements ProjectInformation {
107 
108         @Override
109         public String getName() {
110             return getProjectDirectory().getName();
111         }
112 
113         @NbBundle.Messages("Work_Project_Information_Name=Work Files")
114         @Override
115         public String getDisplayName() {
116             return Bundle.Work_Project_Information_Name();
117         }
118 
119         @Override
120         public Icon getIcon() {
121             return new ImageIcon(ImageUtilities.loadImage(PROJECT_ICON));
122         }
123 
124         @Override
125         public Project getProject() {
126             return WorkProject.this;
127         }
128 
129         @Override
130         public void addPropertyChangeListener(
131                 final PropertyChangeListener listener) { }
132 
133         @Override
134         public void removePropertyChangeListener(
135                 final PropertyChangeListener listener) { }
136     }
137 
138     /**
139      * <p>WorkProjectLogicalView class.</p>
140      */
141     private static class WorkProjectLogicalView implements LogicalViewProvider {
142 
143         /** WorkProject <code>project</code>. */
144         private final WorkProject project;
145 
146         /**
147          *
148          * @param workProject WorkProject
149          */
150         public WorkProjectLogicalView(final WorkProject workProject) {
151             this.project = workProject;
152         }
153 
154         @Override
155         public Node createLogicalView() {
156             try {
157                 //Obtain the project directory's node:
158                 FileObject projectDirectory = project.getProjectDirectory();
159                 DataFolder projectFolder = DataFolder.findFolder(projectDirectory);
160                 Node nodeOfProjectFolder = projectFolder.getNodeDelegate();
161 
162                 //Decorate the project directory's node:
163                 return new ProjectNode(nodeOfProjectFolder, project);
164 
165             } catch (DataObjectNotFoundException donfe) {
166                 Exceptions.printStackTrace(donfe);
167                 //Fallback-the directory couldn't be created -
168                 //read-only filesystem or something evil happened
169                 return new AbstractNode(Children.LEAF);
170             }
171         }
172 
173         @Override
174         public Node findPath(final Node root, final Object target) {
175             //leave unimplemented for now
176             return null;
177         }
178 
179         /**
180          * <p>ProjectNode class.</p>
181          */
182         private static class ProjectNode extends FilterNode {
183 
184             /** WorkProject <code>project</code>. */
185             private final WorkProject project;
186 
187             /**
188              *
189              * @param node Node
190              * @param workProject WorkProject
191              * @throws DataObjectNotFoundException Exception
192              */
193             public ProjectNode(final Node node, final WorkProject workProject)
194                     throws DataObjectNotFoundException {
195                 super(node,
196                         NodeFactorySupport.createCompositeChildren(
197                         workProject,
198                         "Projects/info-mikethomas-fahview-work/Nodes"),
199                         new ProxyLookup(
200                         new Lookup[]{
201                     Lookups.singleton(workProject),
202                     node.getLookup()
203                 }));
204 
205                 this.project = workProject;
206             }
207 
208             @Override
209             public Action[] getActions(final boolean arg0) {
210                 return new Action[]{
211                     CommonProjectActions.deleteProjectAction(),
212                     CommonProjectActions.closeProjectAction()
213                 };
214             }
215 
216             @Override
217             public Image getIcon(final int type) {
218                 return ImageUtilities.loadImage(PROJECT_ICON);
219             }
220 
221             @Override
222             public Image getOpenedIcon(final int type) {
223                 return getIcon(type);
224             }
225 
226             @Override
227             public String getDisplayName() {
228                 return project.getProjectDirectory().getName();
229             }
230         }
231     }
232 }