View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.gui.views.helpers;
20  
21  import java.awt.Color;
22  import java.awt.Font;
23  import java.awt.GridBagConstraints;
24  import java.awt.Insets;
25  
26  import javax.swing.BorderFactory;
27  import javax.swing.JLabel;
28  import javax.swing.JPanel;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTextArea;
31  import javax.swing.ScrollPaneConstants;
32  
33  import jgroup.relacs.config.ExperimentConfig;
34  
35  import org.w3c.dom.CDATASection;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  
39  
40  /**
41   * @author Bjarte Svaeren
42   */
43  public class ScriptPanelManager implements PanelManager
44  {
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // GUI fields
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49    // The panel to managed:
50    private JPanel      scriptPanel = null;
51    
52    private JLabel      scriptLabel;
53    private JTextArea   scriptArea;
54    private JScrollPane scriptPane;
55  
56  
57    ////////////////////////////////////////////////////////////////////////////////////////////
58    // Data fields
59    ////////////////////////////////////////////////////////////////////////////////////////////
60  
61    private String script;
62    
63    
64    ////////////////////////////////////////////////////////////////////////////////////////////
65    // Constructor
66    ////////////////////////////////////////////////////////////////////////////////////////////
67  
68    public ScriptPanelManager(JPanel parentPanel) 
69    {
70      scriptPanel = parentPanel;
71    }
72  
73  
74    ////////////////////////////////////////////////////////////////////////////////////////////
75    // Implemented methods from interface PanelManager
76    ////////////////////////////////////////////////////////////////////////////////////////////
77  
78    /* (non-Javadoc)
79     *    * @see jgroup.experiment.gui.views.helpers.PanelManager#makePanel()
80     */
81    public void makePanel()
82    {
83      initComponents();
84      addComponents();
85    }
86  
87  
88    /* (non-Javadoc)
89     * @see jgroup.experiment.gui.views.helpers.PanelManager#updatePanel(jgroup.relacs.config.ExperimentConfig)
90     */
91    public void updatePanel(ExperimentConfig expConfig)
92    {
93      script = expConfig.getRepeatScript();
94      cleanScript();
95      scriptArea.setText(script);
96    }
97  
98  
99    /* (non-Javadoc)
100    * @see jgroup.experiment.gui.views.helpers.PanelManager#setParentPanel(javax.swing.JPanel)
101    */
102   public void setParentPanel(JPanel panel)
103   {
104     if(scriptPanel == null)
105       throw new IllegalArgumentException("JPanel expPanel has already been set.");
106       
107     scriptPanel = panel;
108   }
109 
110 
111   /* (non-Javadoc)
112    * @see jgroup.experiment.gui.views.helpers.PanelManager#clear()
113    */
114   public void clear()
115   {
116     scriptArea.setText("");
117   }
118 
119 
120   public void save(Element parentElement, Document document)
121   {
122     Element repeatScriptElement = document.createElement("RepeatScript");
123     CDATASection cdata = document.createCDATASection("\n" + scriptArea.getText());
124     repeatScriptElement.appendChild(cdata);
125     parentElement.appendChild(repeatScriptElement);
126   }
127 
128 
129   ////////////////////////////////////////////////////////////////////////////////////////////
130   // Private methods
131   ////////////////////////////////////////////////////////////////////////////////////////////
132 
133   private void initComponents() {
134     scriptPanel.setBorder(BorderFactory.createCompoundBorder(
135                           BorderFactory.createEtchedBorder(),
136                           BorderFactory.createEmptyBorder(5,5,5,5)));                           
137     
138     scriptLabel = new JLabel("Repeat script:");
139     scriptLabel.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
140     
141     scriptArea  = new JTextArea();
142     scriptArea.setFont(new Font("Monospace", Font.BOLD, 12));
143 
144     scriptPane  = new JScrollPane(scriptArea);
145     scriptPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
146     scriptPane.setBorder(BorderFactory.createCompoundBorder(
147                             BorderFactory.createLineBorder(Color.BLACK,2),
148                             BorderFactory.createEtchedBorder()));
149   }
150 
151 
152   private void addComponents() {
153     GridBagConstraints gbc = new GridBagConstraints();
154     
155     gbc.anchor    = GridBagConstraints.FIRST_LINE_START;
156     scriptPanel.add(scriptLabel, gbc);
157     
158     gbc.gridy     = 1;
159     gbc.anchor    = GridBagConstraints.CENTER;
160     gbc.fill      = GridBagConstraints.BOTH;
161     gbc.weightx   = 0.1;
162     gbc.weighty   = 0.1;
163     gbc.insets    = new Insets(5,20,20,20);
164     scriptPanel.add(scriptPane, gbc);
165   }
166   
167   
168   private void cleanScript()
169   {
170     // Replace tabs with spaces
171     script = script.replaceAll("\t", " ");    
172     // Split the script into lines
173     String[] scriptLines = script.split("\n");
174     char[] chars = null;
175 
176     // Find the line and character index of the
177     // first non-space characater
178     int lineIndex      = -1;
179     int characterIndex = -1;
180     for (int i = 0; i < scriptLines.length; i++) {
181       chars = scriptLines[i].toCharArray();
182       for (int j = 0; j < chars.length; j++) {
183         if(Character.isWhitespace(chars[j]) == false) {
184           lineIndex      = i;
185           characterIndex = j;
186           break;
187         }        
188       }  
189       
190       if(lineIndex > -1)
191         break;          
192     }
193 
194     // Remove the leading linges without text
195     // Remove leading spaces
196     // Then append the lines to the StringBuilder
197     StringBuilder temp = new StringBuilder();
198     for (int i = lineIndex; i < scriptLines.length; i++) {
199       if(scriptLines[i].length() > characterIndex)
200         temp.append(scriptLines[i].substring(characterIndex) + '\n');
201       else
202         temp.append(scriptLines[i]);
203     }
204     
205     
206     // Update the script
207     script = new String(temp);
208   }
209 }