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.util.ArrayList;
25  
26  import javax.swing.Box;
27  import javax.swing.Icon;
28  import javax.swing.ImageIcon;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.border.EmptyBorder;
32  
33  import org.bounce.FormConstraints;
34  import org.bounce.FormLayout;
35  import org.bounce.image.ImageLoader;
36  import org.xmlhammer.gui.history.HistoryComboBox;
37  import org.xmlhammer.gui.history.HistoryComboBoxModel;
38  import org.xmlhammer.gui.history.HistoryUtilities;
39  import org.xmlhammer.gui.overview.ComboBoxNode;
40  import org.xmlhammer.gui.overview.OverviewNode;
41  import org.xmlhammer.model.tools.xslt.Associated;
42  
43  /***
44   * Input Panel.
45   * 
46   * Allows to select either one URI, multiple URIs or 
47   * a range of files.
48   * 
49   * @version $Revision$, $Date$
50   * @author Edwin Dankert <edankert@gmail.com>
51   */
52  
53  public class AssociatedStylesheetPanel extends JPanel {
54      private static final long serialVersionUID = -277458243364064175L;
55  
56      private static final ImageIcon PARAMETER_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/eview16/stackframe.gif");
57  
58      private ArrayList<OverviewNode> nodes      = null;
59      private StylesheetsPage parent = null;
60  
61  	private HistoryComboBox mediaField = null;
62      private HistoryComboBox titleField = null;
63      private HistoryComboBox charsetField  = null;
64      private ParametersPanel parametersPanel  = null;
65  
66      /***
67  	 * Constructs a new Input Panel.
68  	 */
69  	public AssociatedStylesheetPanel(StylesheetsPage page) {
70  		super( new FormLayout( 11, 5));
71  		
72  		setBorder( new EmptyBorder( 10, 10, 10, 10));
73          
74          this.parent = page;
75          
76          mediaField = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getMedias()));
77          
78          if (parent != null) {
79              parent.getProjectView().getFieldManager().addField(mediaField);
80          }
81  
82          add(new JLabel("media:"), new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
83          add(mediaField, FormLayout.RIGHT_FILL);
84  
85          titleField = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getTitles()));
86          
87          if (parent != null) {
88              parent.getProjectView().getFieldManager().addField(titleField);
89          }
90  
91          add(new JLabel("title:"), new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
92          add(titleField, FormLayout.RIGHT_FILL);
93  
94          charsetField = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getCharsets()));
95  
96          if (parent != null) {
97              parent.getProjectView().getFieldManager().addField(charsetField);
98          }
99  
100         add(new JLabel("charset:"), new FormConstraints( FormConstraints.LEFT, FormConstraints.RIGHT));
101         add(charsetField, FormLayout.RIGHT_FILL);
102         
103         add(Box.createVerticalStrut(10), FormLayout.FULL);
104 
105         ParametersNode parametersNode = new ParametersNode();
106         parametersPanel = new ParametersPanel(page, parametersNode);
107 
108         add(new JLabel("Parameters:"), FormLayout.FULL);
109         add(parametersPanel, FormLayout.FULL_FILL);
110 
111         nodes = new ArrayList<OverviewNode>();
112         nodes.add(new ComboBoxNode(parent, mediaField, "media"));
113         nodes.add(new ComboBoxNode(parent, titleField, "title"));
114         nodes.add(new ComboBoxNode(parent, charsetField, "charset"));
115         nodes.add(parametersNode);
116    	}
117     
118 	public void setAssociated(Associated associated) {
119         titleField.setSelectedItem(associated.getTitle());
120         mediaField.setSelectedItem(associated.getMedia());
121         charsetField.setSelectedItem(associated.getCharset());
122         parametersPanel.setParameters(associated.getParameter());
123 	}
124 	
125 	public Associated getAssociated() {
126         Associated associated = new Associated();
127         
128         associated.setTitle(getValue(titleField));
129         associated.setMedia(getValue(mediaField));
130         associated.setCharset(getValue(charsetField));
131         associated.getParameter().addAll(parametersPanel.getParameters());
132 
133         return associated;
134 	}
135     
136     private static String getValue(HistoryComboBox box) {
137         Object item = box.getSelectedItem();
138         
139         if (item != null && item.toString().trim().length() > 0) {
140             return item.toString();
141         }
142 
143         return null;
144     }
145 	
146     public ArrayList<OverviewNode> getNodes() {
147         ArrayList<OverviewNode> list = new ArrayList<OverviewNode>(nodes);
148         list.addAll(parametersPanel.getNodes());
149         return list;
150     }
151     
152     public class ParametersNode implements OverviewNode {
153         
154         public String getNodeName() {
155             return "Parameters";
156         }
157 
158         public ArrayList<OverviewNode> getChildNodes() {
159             return parametersPanel.getNodes();
160         }
161 
162         public OverviewNode getParentNode() {
163             return parent;
164         }
165 
166         public Icon getNodeIcon() {
167             return PARAMETER_ICON;
168         }
169         
170         public String getError() {
171             return null;
172         }
173     }
174 }