KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > vwmodel > Page


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.jsf.navigation.vwmodel;
20
21 import org.netbeans.modules.web.jsf.navigation.vwmodel.GraphUtilities;
22 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigableComponent;
23 import org.netbeans.modules.web.jsf.navigation.*;
24 import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
25 import java.awt.Image JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
33 import org.netbeans.modules.visualweb.insync.models.FacesModel;
34
35
36 /**
37  * graph node
38  */

39 public class Page {
40
41     public static final int BUTTON = 1;
42     public static final int HYPERLINK = 2;
43     public static final int IMAGE_HYPERLINK = 3;
44
45     private String JavaDoc buttonClass_bh = "com.sun.rave.web.ui.component.Button";
46     private String JavaDoc hyperlinkClass_bh = "com.sun.rave.web.ui.component.Hyperlink";
47     private String JavaDoc imageHyperlinkClass_bh = "com.sun.rave.web.ui.component.ImageHyperlink";
48
49     private String JavaDoc buttonClass_ws = "com.sun.webui.jsf.component.Button";
50     private String JavaDoc hyperlinkClass_ws = "com.sun.webui.jsf.component.Hyperlink";
51     private String JavaDoc imageHyperlinkClass_ws = "com.sun.webui.jsf.component.ImageHyperlink";
52
53     private NavigableComponent currentBean = null;
54
55
56     public Page(String JavaDoc name, FileObject fileObject, NavigationModel doc) {
57         this.setName(name);
58         projectFileObject = fileObject;
59         if (fileObject != null) {
60             model = ((FacesModelSet)doc.getOwner()).getFacesModel(fileObject);
61             if (model != null) {
62                 wasModelBusted = model.isBusted();
63             }
64             try {
65                 this.dobj = DataObject.find(fileObject);
66                 restoreLocation();
67             } catch (DataObjectNotFoundException e) {
68             }
69         }
70         this.doc = doc;
71     }
72
73     private String JavaDoc name;
74     private NavigationModel doc;
75     private DataObject dobj;
76     private FileObject projectFileObject;
77     private List JavaDoc<NavigableComponent> beans = new ArrayList JavaDoc(); // Components on this page
78
private List JavaDoc<NavigableComponentLink> beanLinks; // Links from beans to ports on this page
79
private Image JavaDoc previewImage;
80     private Image JavaDoc previewZoomedImage;
81
82     FacesModel model;
83     // !EAT TODO Do we need to update this flag on changes in model, or is Page object rebuilt in those cases anyway ?
84
protected boolean wasModelBusted;
85
86     // Layout information - for performance reasons provided here
87
// for direct manipulation by the layout algorithms so they don't
88
// have to duplicate data structures to annotate elements with
89
// layout info and temporary tags etc.
90

91     private int x = -1; // layout position
92
private int y = -1;
93     private int width;
94     private int height;
95     private int adjustmentX;
96     private int adjustmentY;
97
98     public NavigableComponent getCurrentBean(){
99         if(currentBean == null){
100             if (!getBeans().isEmpty()){
101                 return (NavigableComponent)getBeans().get(0);
102             }
103         }
104         return currentBean;
105     }
106
107     public void setCurrentBean(NavigableComponent bean){
108         currentBean = bean;
109     }
110
111     public void moveCurrentBean(int dir){
112         if(!getBeans().isEmpty()){
113             int currIndex = getBeans().indexOf(getCurrentBean());
114             switch(dir){
115                 case GraphUtilities.DOWN:
116                     currIndex++;
117                     if(currIndex >= getBeans().size()){
118                         currIndex = 0;
119                     }
120                     break;
121                 case GraphUtilities.UP:
122                     currIndex--;
123                     if(currIndex < 0){
124                         currIndex = getBeans().size()-1;
125                     }
126             }
127             currentBean = (NavigableComponent)getBeans().get(currIndex);
128         }
129     }
130
131     public List JavaDoc<NavigableComponent> getBeans(){
132 // return (NavigableComponent[]) beans.toArray(new NavigableComponent[beans.size()]);
133
return beans;
134     }
135     
136     public void setAdjustmentX(int adjustmentX) {
137         this.adjustmentX = adjustmentX;
138     }
139     public void setAdjustmentY(int adjustmentY) {
140         this.adjustmentY = adjustmentY;
141     }
142     public void setX(int x) {
143         if(x < -1) x = 0;
144         this.x = x;
145     }
146     public void setY(int y) {
147         if(y < -1) y = 0;
148         this.y = y;
149     }
150     public void setWidth(int w) {
151         this.width = w;
152     }
153     public void setHeight(int h) {
154         this.height = h;
155     }
156     
157     public int x() {
158         if (adjustmentX == 0) {
159             return x;
160         } else {
161             int adjusted = x-adjustmentX;
162             if (adjusted < GraphUtilities.LEFT_OFFSET) {
163                 return GraphUtilities.LEFT_OFFSET;
164             }
165             return adjusted;
166         }
167     }
168     
169     public int y() {
170         if (adjustmentY == 0) {
171             return y;
172         } else {
173             int adjusted = y-adjustmentY;
174             if (adjusted < GraphUtilities.TOP_OFFSET) {
175                 return GraphUtilities.TOP_OFFSET;
176             }
177             return adjusted;
178         }
179     }
180     
181     public int w() { // be consistent with setWidth - use setW() or getWidth()
182
if (adjustmentX == 0) {
183             return width;
184         } else {
185             return width + 2*adjustmentX;
186         }
187     }
188     
189     public int h() { // ditto
190
if (adjustmentY == 0) {
191             return height;
192         } else {
193             return height + 2*adjustmentY;
194         }
195     }
196     
197     /**
198      * Return adjust height based on number of beans in the page
199      */

200     public int h(boolean adjust) { // ditto
201
int adjustedHeight = 0;
202         
203         int n = getBeans().size();
204         for (int i = 0; i < n; i++) {
205             NavigableComponent pageBean = (NavigableComponent)getBeans().get(i);
206             if (adjustedHeight < pageBean.ly){
207                 adjustedHeight = pageBean.ly + pageBean.lh;
208             }
209         }
210         adjustedHeight += 10;
211         if (adjustedHeight < height) adjustedHeight = height;
212         if (adjustmentY == 0) {
213             return adjustedHeight;
214         } else {
215             return adjustedHeight + 2*adjustmentY;
216         }
217     }
218     
219     public FacesModel getModel() {
220         return model;
221     }
222     
223     public boolean wasModelBusted() {
224         return wasModelBusted;
225     }
226     
227     // Try to get the location from project using GenericItem
228
public void restoreLocation() {
229         // Dont think I want to store it there ?
230
String JavaDoc locationProperty = (String JavaDoc) projectFileObject.getAttribute("NavigationPageLocation");
231         if (locationProperty != null) {
232             String JavaDoc[] values = locationProperty.split(":");
233             try {
234                 setX(Integer.parseInt(values[0]));
235                 setY(Integer.parseInt(values[1]));
236             } catch (Exception JavaDoc exc) {
237                 exc.printStackTrace();
238             }
239         } else {
240             setX(-1);
241             setY(-1);
242         }
243     }
244     
245     // Save the location to project using GenericItem
246
public void saveLocation() {
247         try {
248             projectFileObject.setAttribute("NavigationPageLocation", x() + ":" + y());
249         } catch (IOException JavaDoc e) {
250         }
251     }
252     
253     int num; // leftmost page is 0, page next to it 1, ....
254
// Only for temporary/scratch use during layout computations
255
int nextPortnum;
256     // incident from:
257
List JavaDoc<Page> pointedTo; // list of pages pointing to this page
258
// incident to:
259
List JavaDoc<Page> pointsTo; // list of pages originating at this page
260
boolean used; // mark during searches to avoid circularity etc.
261

262     int beanHeight; // When showing beans, use this height if > h()
263
// this allows a component to have a large number of beans
264
// where the page scales
265

266     public String JavaDoc toString() {
267         return "Page[" + getName() +","+x+","+y+","+width+","+height+","+num+
268                 //",pointsTo="+pointsTo+",pointedTo="+pointedTo+","+used+
269
"]";
270     }
271     
272     public String JavaDoc getName() {
273         return this.name;
274     }
275     
276     public void setName(String JavaDoc name) {
277         this.name = name;
278     }
279     
280     public DataObject getDataObject() {
281         return getDobj();
282     }
283     
284     public void setDataObject(DataObject dob) {
285         dobj = dob;
286     }
287     
288     public int getBeanCount(){
289         return getBeans().size();
290     }
291     
292     public boolean hasBeans(){
293         return !getBeans().isEmpty();
294     }
295     
296     public NavigableComponent getBean(int index){
297         return (NavigableComponent) getBeans().get(index);
298     }
299     
300     public String JavaDoc getNameExt(){
301         return getDobj().getPrimaryFile().getNameExt();
302     }
303     
304     String JavaDoc getBeanClassName(int type) {
305         String JavaDoc javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getDoc().getProject());
306         switch (type){
307             case BUTTON:
308                 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){
309                     return buttonClass_ws;
310                 }else{
311                     return buttonClass_bh;
312                 }
313             case HYPERLINK:
314                 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){
315                     return hyperlinkClass_ws;
316                 }else{
317                     return hyperlinkClass_bh;
318                 }
319             case IMAGE_HYPERLINK:
320                 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){
321                     return imageHyperlinkClass_ws;
322                 }else{
323                     return imageHyperlinkClass_bh;
324                 }
325         }
326         return null;
327     }
328
329     protected NavigationModel getDoc() {
330         return doc;
331     }
332
333     protected DataObject getDobj() {
334         return dobj;
335     }
336
337     protected void setBeans(List JavaDoc<NavigableComponent> beans) {
338         this.beans = beans;
339     }
340
341     public List JavaDoc getBeanLinks() {
342         return beanLinks;
343     }
344
345     public void setBeanLinks(List JavaDoc beanLinks) {
346         this.beanLinks = beanLinks;
347     }
348
349     protected Image JavaDoc getPreviewImage() {
350         return previewImage;
351     }
352
353     protected void setPreviewImage(Image JavaDoc previewImage) {
354         this.previewImage = previewImage;
355     }
356
357     protected Image JavaDoc getPreviewZoomedImage() {
358         return previewZoomedImage;
359     }
360
361     protected void setPreviewZoomedImage(Image JavaDoc previewZoomedImage) {
362         this.previewZoomedImage = previewZoomedImage;
363     }
364 }
365
Popular Tags