View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.xslt;
23  
24  import java.awt.event.ItemEvent;
25  import java.net.URI;
26  import java.util.ArrayList;
27  
28  import javax.swing.JPanel;
29  
30  import org.xmlhammer.gui.ProjectView;
31  import org.xmlhammer.gui.overview.OverviewNode;
32  import org.xmlhammer.gui.preferences.PropertiesDialog;
33  import org.xmlhammer.gui.util.SelectionPage;
34  import org.xmlhammer.model.jaxp.Settings;
35  import org.xmlhammer.model.project.Document;
36  import org.xmlhammer.model.project.ParameterisedSource;
37  import org.xmlhammer.model.tools.xslt.Stylesheets;
38  import org.xmlhammer.model.tools.xslt.XSLT.Transform;
39  
40  /***
41   * Input Panel.
42   * 
43   * Allows to select either one URI, multiple URIs or 
44   * a range of files.
45   * 
46   * @version $Revision$, $Date$
47   * @author Edwin Dankert <edankert@gmail.com>
48   */
49  
50  public class StylesheetsPage extends SelectionPage {
51      private static final long serialVersionUID = -6021755010826846525L;
52  
53      private boolean initialised = false;
54  
55      private PropertiesDialog propertiesDialog   = null;
56      private StylesheetURIsPanel urisPanel       = null;
57      private StylesheetURIsPanel nourisPanel     = null;
58      private AssociatedStylesheetPanel associatedStylesheetPanel     = null;
59  
60  	/***
61  	 * @param view the underlying view.
62  	 */
63  	public StylesheetsPage( ProjectView view) {
64  		super( view, "Transformer:", true);
65          
66          urisPanel = new StylesheetURIsPanel(this);
67          nourisPanel = new StylesheetURIsPanel(this);
68          nourisPanel.setEnabled(false);
69          
70          associatedStylesheetPanel = new AssociatedStylesheetPanel(this);
71  
72          addPanel("Default Transform", nourisPanel);
73          addPanel("Select Stylesheet URI(s)", urisPanel);
74          addPanel("Associated Stylesheet", associatedStylesheetPanel);
75  
76          initialised = true;
77  	}
78  	
79  	/***
80  	 * Shows the properties dialog.
81  	 */
82  	public void showPropertiesDialog() {
83  		Settings settings = getProjectView().getProject(null).getJAXPSettings();
84  
85  		if (propertiesDialog == null) {
86  			propertiesDialog = new PropertiesDialog(getProjectView().getProjectsView().getRoot(), false, false, false, true, false);
87  		}
88  
89          propertiesDialog.setTransformerFactory(settings.getJAXPTransformerFactory());
90  
91          if (propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
92              settings.setJAXPTransformerFactory(propertiesDialog.getTransformerFactory());
93              getProjectView().getFieldManager().setPropertiesChanged(true);
94          }
95  	}
96  
97      public String getShortName() { 
98          return "Transformer";
99      }
100 
101     public String getNodeName() { 
102         JPanel panel = getSelectedPanel();
103 
104         if (panel instanceof StylesheetURIsPanel) {
105             if (((StylesheetURIsPanel)panel).isEnabled()) {
106                 return "Transformer (Select URIs)";
107             }
108             
109             return "Transformer (Default)";
110         } 
111         
112         return "Transformer (Associated)";
113 	}
114 	
115 	/* (non-Javadoc)
116 	 * @see org.xmlhammer.gui.Page#getChildNodes()
117 	 */
118 	public ArrayList<OverviewNode> getChildNodes() {
119         JPanel panel = getSelectedPanel();
120         
121         if (panel == urisPanel) {
122             return urisPanel.getChildNodes();
123         } else if (panel == nourisPanel) {
124             return new ArrayList<OverviewNode>();
125         } else {
126             return associatedStylesheetPanel.getNodes();
127         }
128 	}
129     
130     public String getError() {
131         return null;
132     }
133 
134     public boolean isAssociatedSelected() {
135         return getSelectedPanel() == associatedStylesheetPanel;    
136     }
137     
138     public boolean isDefaultSelected() {
139         return getSelectedPanel() == nourisPanel;    
140     }
141 
142     public boolean isStylesheetsSelected() {
143         return getSelectedPanel() == urisPanel;    
144     }
145 
146     public void setTransform(URI base, Transform transform) {
147         if (transform != null) {
148             if (transform.getAssociated() != null) {
149                 setSelectedPanel(associatedStylesheetPanel);
150                 associatedStylesheetPanel.setAssociated(transform.getAssociated());
151             } else if (transform.getStylesheets() != null) {
152                 setSelectedPanel(urisPanel);
153                 urisPanel.setURIs(base, transform.getStylesheets().getParameterisedSource());
154             } else {
155                 setSelectedPanel(nourisPanel);
156             }
157         } else {
158             setSelectedPanel(nourisPanel);
159         }
160     }
161     
162     public Transform getTransform(URI base) { 
163         Transform transform = new Transform();
164         
165         JPanel panel = getSelectedPanel();
166         
167         if (panel == urisPanel) {
168             Stylesheets stylesheets = new Stylesheets();
169             transform.setStylesheets(stylesheets);
170             for (Document source : urisPanel.getURIs(base)) {
171                 stylesheets.getParameterisedSource().add((ParameterisedSource)source);
172             }
173         } else if (panel == associatedStylesheetPanel) {
174             transform.setAssociated(associatedStylesheetPanel.getAssociated());
175         }
176 
177         return transform;
178     }
179 
180     public void itemStateChanged(ItemEvent e) {
181         super.itemStateChanged( e);
182         
183         if ( initialised) {
184             ProjectView view = getProjectView();
185 
186             // Need to stay here, otherwise the children are not de-selected.
187             view.getOverviewPanel().selectNode(this);
188             view.getOverviewPanel().getModel().structureChanged(this);
189             view.getOverviewPanel().expandAll();
190         }
191     }
192 
193     public void dispose() {
194     }
195 
196     @Override
197     public String getHelpID() {
198         return getProjectView().getHelpID()+".specification";
199     }
200 }