001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2007 Dennis Reil
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.installer;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Container;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.MouseEvent;
029: import java.awt.event.MouseListener;
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: import javax.swing.BorderFactory;
036: import javax.swing.BoxLayout;
037: import javax.swing.JButton;
038: import javax.swing.JFrame;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.JTabbedPane;
043: import javax.swing.JTable;
044: import javax.swing.JTextField;
045: import javax.swing.JTextPane;
046: import javax.swing.ListSelectionModel;
047:
048: import com.izforge.izpack.Panel;
049: import com.izforge.izpack.gui.ButtonFactory;
050: import com.izforge.izpack.gui.IconsDatabase;
051: import com.izforge.izpack.rules.Condition;
052: import com.izforge.izpack.rules.RulesEngine;
053:
054: /**
055: * Class for debugging variables and conditions.
056: * @author Dennis Reil, <Dennis.Reil@reddot.de>
057: * @version $Id: $
058: */
059: public class Debugger {
060: private RulesEngine rules;
061: private InstallData idata;
062:
063: private Properties lasttimevariables;
064:
065: private JTextPane debugtxt;
066: private IconsDatabase icons;
067: private Map<String, VariableHistory> variableshistory;
068: private Map<String, ConditionHistory> conditionhistory;
069:
070: private JTable variablestable;
071: private VariableHistoryTableModel variablesmodel;
072: private VariableHistoryTableCellRenderer variablesrenderer;
073: private ConditionHistoryTableModel conditionhistorymodel;
074: private ConditionHistoryTableCellRenderer conditionhistoryrenderer;
075:
076: public Debugger(InstallData installdata, IconsDatabase icons,
077: RulesEngine rules) {
078: idata = installdata;
079: this .rules = rules;
080: lasttimevariables = (Properties) idata.variables.clone();
081: this .icons = icons;
082: this .variableshistory = new HashMap<String, VariableHistory>();
083: this .conditionhistory = new HashMap<String, ConditionHistory>();
084: this .init();
085: }
086:
087: private void init() {
088: String[] variablekeys = (String[]) lasttimevariables.keySet()
089: .toArray(new String[lasttimevariables.size()]);
090: for (String variablename : variablekeys) {
091: VariableHistory vh = new VariableHistory(variablename);
092: vh.addValue(lasttimevariables.getProperty(variablename),
093: "initial value");
094: variableshistory.put(variablename, vh);
095: }
096: String[] conditionids = this .rules.getKnownConditionIds();
097: for (String conditionid : conditionids) {
098: Condition currentcondition = RulesEngine
099: .getCondition(conditionid);
100: boolean result = this .rules
101: .isConditionTrue(currentcondition);
102:
103: ConditionHistory ch = null;
104: ch = new ConditionHistory(currentcondition);
105:
106: ch.addValue(result, "initial value");
107: conditionhistory.put(conditionid, ch);
108:
109: }
110: }
111:
112: private void debugVariables(Panel nextpanelmetadata,
113: Panel lastpanelmetadata) {
114: getChangedVariables(nextpanelmetadata, lastpanelmetadata);
115: lasttimevariables = (Properties) idata.variables.clone();
116: }
117:
118: private void debugConditions(Panel nextpanelmetadata,
119: Panel lastpanelmetadata) {
120: conditionhistoryrenderer.clearState();
121: updateChangedConditions("changed after panel switch from "
122: + lastpanelmetadata.getPanelid() + " to "
123: + nextpanelmetadata.getPanelid());
124: }
125:
126: private void updateChangedConditions(String comment) {
127: String[] conditionids = this .rules.getKnownConditionIds();
128: for (String conditionid : conditionids) {
129: Condition currentcondition = RulesEngine
130: .getCondition(conditionid);
131: ConditionHistory ch = null;
132: if (!conditionhistory.containsKey(conditionid)) {
133: // new condition
134: ch = new ConditionHistory(currentcondition);
135: conditionhistory.put(conditionid, ch);
136: } else {
137: ch = conditionhistory.get(conditionid);
138: }
139: ch.addValue(this .rules.isConditionTrue(currentcondition),
140: comment);
141: }
142: conditionhistorymodel.fireTableDataChanged();
143: }
144:
145: private Properties getChangedVariables(Panel nextpanelmetadata,
146: Panel lastpanelmetadata) {
147: Properties currentvariables = (Properties) idata.variables
148: .clone();
149: Properties changedvariables = new Properties();
150:
151: variablesrenderer.clearState();
152: // check for changed and new variables
153: Enumeration currentvariableskeys = currentvariables.keys();
154: boolean changes = false;
155: while (currentvariableskeys.hasMoreElements()) {
156: String key = (String) currentvariableskeys.nextElement();
157: String currentvalue = currentvariables.getProperty(key);
158: String oldvalue = lasttimevariables.getProperty(key);
159:
160: if ((oldvalue == null)) {
161: VariableHistory vh = new VariableHistory(key);
162: vh.addValue(currentvalue, "new after panel "
163: + lastpanelmetadata.getPanelid());
164: variableshistory.put(key, vh);
165: changes = true;
166: changedvariables.put(key, currentvalue);
167: } else {
168: if (!currentvalue.equals(oldvalue)) {
169: VariableHistory vh = variableshistory.get(key);
170: vh.addValue(currentvalue,
171: "changed value after panel "
172: + lastpanelmetadata.getPanelid());
173: changes = true;
174: changedvariables.put(key, currentvalue);
175: }
176: }
177: }
178: if (changes) {
179: variablesmodel.fireTableDataChanged();
180: }
181: return changedvariables;
182: }
183:
184: private void modifyVariableManually(String varnametxt,
185: String varvaluetxt) {
186: lasttimevariables = (Properties) idata.variables.clone();
187: VariableHistory vh = variableshistory.get(varnametxt);
188: if (vh != null) {
189: vh.addValue(varvaluetxt, "modified manually");
190: }
191: variablesmodel.fireTableDataChanged();
192: updateChangedConditions("after manual modification of variable "
193: + varnametxt);
194: }
195:
196: public JPanel getDebugPanel() {
197: JPanel debugpanel = new JPanel();
198: debugpanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0,
199: 10));
200: debugpanel.setLayout(new BorderLayout());
201:
202: variablesmodel = new VariableHistoryTableModel(variableshistory);
203: variablesrenderer = new VariableHistoryTableCellRenderer(
204: variableshistory);
205: variablestable = new JTable(variablesmodel);
206: variablestable.setDefaultRenderer(VariableHistory.class,
207: variablesrenderer);
208: variablestable
209: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
210: variablestable.setRowSelectionAllowed(true);
211:
212: JScrollPane scrollpane = new JScrollPane(variablestable);
213:
214: debugpanel.add(scrollpane, BorderLayout.CENTER);
215:
216: JPanel varchangepanel = new JPanel();
217: varchangepanel.setLayout(new BoxLayout(varchangepanel,
218: BoxLayout.LINE_AXIS));
219:
220: final JTextField varname = new JTextField();
221: varchangepanel.add(varname);
222: JLabel label = new JLabel("=");
223: varchangepanel.add(label);
224: final JTextField varvalue = new JTextField();
225: varchangepanel.add(varvalue);
226: JButton changevarbtn = ButtonFactory.createButton(
227: idata.langpack.getString("debug.changevariable"), icons
228: .getImageIcon("debug.changevariable"),
229: idata.buttonsHColor);
230: changevarbtn.addActionListener(new ActionListener() {
231:
232: public void actionPerformed(ActionEvent e) {
233: String varnametxt = varname.getText();
234: String varvaluetxt = varvalue.getText();
235: if ((varnametxt != null) && (varnametxt.length() > 0)) {
236: if ((varvaluetxt != null)
237: && (varvaluetxt.length() > 0)) {
238: idata.setVariable(varnametxt, varvaluetxt);
239: modifyVariableManually(varnametxt, varvaluetxt);
240: }
241: }
242: }
243: });
244: variablestable.addMouseListener(new MouseListener() {
245:
246: public void mouseClicked(MouseEvent e) {
247: int selectedrow = variablestable.getSelectedRow();
248: String selectedvariable = (String) variablesmodel
249: .getValueAt(selectedrow, 0);
250:
251: if (e.getClickCount() == 1) {
252: varname.setText(selectedvariable);
253: } else {
254: VariableHistory vh = variableshistory
255: .get(selectedvariable);
256:
257: JFrame variabledetails = new JFrame("Details");
258:
259: JTextPane detailspane = new JTextPane();
260: detailspane.setContentType("text/html");
261: detailspane.setText(vh.getValueHistoryDetails());
262: detailspane.setEditable(false);
263: JScrollPane scroller = new JScrollPane(detailspane);
264:
265: Container con = variabledetails.getContentPane();
266: con.setLayout(new BorderLayout());
267: con.add(scroller, BorderLayout.CENTER);
268:
269: variabledetails.pack();
270: variabledetails.setVisible(true);
271: }
272: }
273:
274: public void mouseEntered(MouseEvent e) {
275: // TODO Auto-generated method stub
276:
277: }
278:
279: public void mouseExited(MouseEvent e) {
280: // TODO Auto-generated method stub
281:
282: }
283:
284: public void mousePressed(MouseEvent e) {
285: // TODO Auto-generated method stub
286:
287: }
288:
289: public void mouseReleased(MouseEvent e) {
290: // TODO Auto-generated method stub
291:
292: }
293:
294: });
295: varchangepanel.add(changevarbtn);
296: debugpanel.add(varchangepanel, BorderLayout.SOUTH);
297:
298: JPanel conditionpanel = new JPanel();
299: conditionpanel.setLayout(new BorderLayout());
300:
301: conditionhistorymodel = new ConditionHistoryTableModel(
302: conditionhistory);
303: final JTable conditiontable = new JTable(conditionhistorymodel);
304: conditionhistoryrenderer = new ConditionHistoryTableCellRenderer(
305: conditionhistory);
306: conditiontable.setDefaultRenderer(ConditionHistory.class,
307: conditionhistoryrenderer);
308: conditiontable
309: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
310: conditiontable.setRowSelectionAllowed(true);
311: conditiontable.addMouseListener(new MouseListener() {
312:
313: public void mouseClicked(MouseEvent e) {
314: int selectedrow = conditiontable.getSelectedRow();
315:
316: String selectedcondition = (String) conditiontable
317: .getModel().getValueAt(selectedrow, 0);
318:
319: if (e.getClickCount() == 2) {
320:
321: ConditionHistory ch = conditionhistory
322: .get(selectedcondition);
323:
324: JFrame variabledetails = new JFrame("Details");
325:
326: JTextPane detailspane = new JTextPane();
327: detailspane.setContentType("text/html");
328: detailspane
329: .setText(ch.getConditionHistoryDetails());
330: detailspane.setEditable(false);
331: JScrollPane scroller = new JScrollPane(detailspane);
332:
333: Container con = variabledetails.getContentPane();
334: con.setLayout(new BorderLayout());
335: con.add(scroller, BorderLayout.CENTER);
336:
337: variabledetails.pack();
338: variabledetails.setVisible(true);
339: }
340:
341: }
342:
343: public void mouseEntered(MouseEvent e) {
344: // TODO Auto-generated method stub
345:
346: }
347:
348: public void mouseExited(MouseEvent e) {
349: // TODO Auto-generated method stub
350:
351: }
352:
353: public void mousePressed(MouseEvent e) {
354: // TODO Auto-generated method stub
355:
356: }
357:
358: public void mouseReleased(MouseEvent e) {
359: // TODO Auto-generated method stub
360:
361: }
362:
363: });
364:
365: JScrollPane conditionscroller = new JScrollPane(conditiontable);
366: conditionpanel.add(conditionscroller, BorderLayout.CENTER);
367:
368: JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
369: tabpane.insertTab("Variable settings", null, debugpanel, "", 0);
370: tabpane.insertTab("Condition settings", null, conditionpanel,
371: "", 1);
372: JPanel mainpanel = new JPanel();
373: mainpanel.setLayout(new BorderLayout());
374: mainpanel.add(tabpane, BorderLayout.CENTER);
375: return mainpanel;
376: }
377:
378: /**
379: * Debug state changes after panel switch.
380: * @param nextpanelmetadata
381: * @param lastpanelmetadata
382: */
383: public void switchPanel(Panel nextpanelmetadata,
384: Panel lastpanelmetadata) {
385: this .debugVariables(nextpanelmetadata, lastpanelmetadata);
386: this .debugConditions(nextpanelmetadata, lastpanelmetadata);
387: }
388:
389: public void packSelectionChanged(String comment) {
390: this.updateChangedConditions(comment);
391: }
392: }
|