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 info.mikethomas.fahview.v6project.properties.ClientCustomizerProvider;
25  import java.awt.Image;
26  import java.beans.PropertyChangeListener;
27  import javax.swing.Action;
28  import javax.swing.Icon;
29  import javax.swing.ImageIcon;
30  import org.netbeans.api.annotations.common.StaticResource;
31  import org.netbeans.api.project.Project;
32  import org.netbeans.api.project.ProjectInformation;
33  import org.netbeans.spi.project.ProjectState;
34  import org.netbeans.spi.project.ui.LogicalViewProvider;
35  import org.netbeans.spi.project.ui.support.CommonProjectActions;
36  import org.netbeans.spi.project.ui.support.NodeFactorySupport;
37  import org.openide.filesystems.FileObject;
38  import org.openide.loaders.DataFolder;
39  import org.openide.loaders.DataObjectNotFoundException;
40  import org.openide.nodes.AbstractNode;
41  import org.openide.nodes.Children;
42  import org.openide.nodes.FilterNode;
43  import org.openide.nodes.Node;
44  import org.openide.util.Exceptions;
45  import org.openide.util.ImageUtilities;
46  import org.openide.util.Lookup;
47  import org.openide.util.lookup.Lookups;
48  import org.openide.util.lookup.ProxyLookup;
49  
50  /**
51   * <p>ClientProject class.</p>
52   *
53   * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
54   * @version $Id: $Id
55   */
56  public class ClientProject implements Project {
57  
58      /** Constant <code>PROJECT_ICON</code>. */
59      @StaticResource()
60      public static final String PROJECT_ICON =
61              "info/mikethomas/fahview/v6project/client.png";
62      /** Constant <code>PROJECT_FILE</code>. */
63      public static final String PROJECT_FILE = "client.cfg";
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 ClientProject.</p>
74       *
75       * @param projectDirectory projectDirectory
76       * @param projectState ProjectState
77       */
78      ClientProject(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 ClientProjectInformation(),
97                  new ClientProjectLogicalView(this),
98                  new ClientCustomizerProvider(this),
99                  new ClientSubprojectProvider(this)
100             });
101         }
102         return lookup;
103     }
104 
105     /**
106      * <p>ClientProjectInformation class.</p>
107      */
108     private final class ClientProjectInformation implements ProjectInformation {
109 
110         @Override
111         public String getName() {
112             return getProjectDirectory().getName();
113         }
114 
115         @Override
116         public String getDisplayName() {
117             return getName();
118         }
119 
120         @Override
121         public Icon getIcon() {
122             return new ImageIcon(ImageUtilities.loadImage(PROJECT_ICON));
123         }
124 
125         @Override
126         public Project getProject() {
127             return ClientProject.this;
128         }
129 
130         @Override
131         public void addPropertyChangeListener(
132                 final PropertyChangeListener listener) { }
133 
134         @Override
135         public void removePropertyChangeListener(
136                 final PropertyChangeListener listener) { }
137     }
138 
139     /**
140      * <p>ClientProjectLogicalView class.</p>
141      */
142     private static class ClientProjectLogicalView
143             implements LogicalViewProvider {
144 
145         /** ClientProject <code>project</code>. */
146         private final ClientProject project;
147 
148         /**
149          *
150          * @param clientProject ClientProject
151          */
152         public ClientProjectLogicalView(final ClientProject clientProject) {
153             this.project = clientProject;
154         }
155 
156         /** {@inheritDoc} */
157         @Override
158         public Node createLogicalView() {
159             try {
160                 //Obtain the project directory's node:
161                 FileObject projectDirectory = project.getProjectDirectory();
162                 DataFolder projectFolder = DataFolder.findFolder(projectDirectory);
163                 Node nodeOfProjectFolder = projectFolder.getNodeDelegate();
164 
165                 //Decorate the project directory's node:
166                 return new ProjectNode(nodeOfProjectFolder, project);
167 
168             } catch (DataObjectNotFoundException donfe) {
169                 Exceptions.printStackTrace(donfe);
170                 //Fallback-the directory couldn't be created -
171                 //read-only filesystem or something evil happened
172                 return new AbstractNode(Children.LEAF);
173             }
174         }
175 
176         @Override
177         public Node findPath(final Node root, final Object target) {
178             //leave unimplemented for now
179             return null;
180         }
181 
182         /**
183          * <p>ProjectNode class.</p>
184          */
185         private static class ProjectNode extends FilterNode {
186 
187             /** ClientProject <code>project</code>. */
188             private final ClientProject project;
189 
190             /**
191              *
192              * @param node Node
193              * @param clientProject ClientProject
194              * @throws DataObjectNotFoundException Exception
195              */
196             public ProjectNode(final Node node,
197                                final ClientProject clientProject)
198                     throws DataObjectNotFoundException {
199 
200                 super(node,
201                         NodeFactorySupport.createCompositeChildren(
202                         clientProject,
203                         "Projects/info-mikethomas-fahview-client/Nodes"),
204                         new ProxyLookup(
205                         new Lookup[]{
206                     Lookups.singleton(clientProject),
207                     node.getLookup()
208                 }));
209 
210                 this.project = clientProject;
211             }
212 
213             @Override
214             public Action[] getActions(final boolean arg0) {
215                 return new Action[]{
216                     CommonProjectActions.customizeProjectAction(),
217                     CommonProjectActions.deleteProjectAction(),
218                     CommonProjectActions.closeProjectAction()
219                 };
220             }
221 
222             @Override
223             public Image getIcon(final int type) {
224                 return ImageUtilities.loadImage(PROJECT_ICON);
225             }
226 
227             @Override
228             public Image getOpenedIcon(final int type) {
229                 return getIcon(type);
230             }
231 
232             @Override
233             public String getDisplayName() {
234                 return project.getProjectDirectory().getName();
235             }
236         }
237     }
238 }