KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > menu > control > PrintAction


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.menu.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.graph.model.Display;
29 import org.objectweb.fractal.gui.model.Component;
30 import org.objectweb.fractal.gui.model.Configuration;
31 import org.objectweb.fractal.gui.graph.view.Printer;
32 import org.objectweb.fractal.gui.selection.model.Selection;
33 import org.objectweb.fractal.swing.AbstractAction;
34
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.List JavaDoc;
38
39 import javax.swing.ImageIcon JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.KeyStroke JavaDoc;
42
43 /**
44  * An action to print a configuration.
45  */

46
47 public class PrintAction extends AbstractAction implements
48   BindingController
49 {
50
51   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
52
53   public final static String JavaDoc SELECTION_BINDING = "selection";
54
55   public final static String JavaDoc DISPLAY_BINDING = "display";
56
57   public final static String JavaDoc PRINTER_BINDING = "printer";
58
59   private Configuration configuration;
60
61   private Selection selection;
62
63   private Display display;
64
65   private Printer printer;
66
67   /**
68    * Constructs a new {@link PrintAction} component.
69    */

70
71   public PrintAction () {
72     putValue(NAME, "Print");
73     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control P"));
74     putValue(SHORT_DESCRIPTION, "Print");
75     URL JavaDoc url = getClass().getResource(
76       "/org/objectweb/fractal/gui/resources/print.gif");
77     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
78     setEnabled(true);
79   }
80
81   // -------------------------------------------------------------------------
82
// Implementation of the BindingController interface
83
// -------------------------------------------------------------------------
84

85   public String JavaDoc[] listFc () {
86     return new String JavaDoc[] {
87       CONFIGURATION_BINDING,
88       SELECTION_BINDING,
89       DISPLAY_BINDING,
90       PRINTER_BINDING
91     };
92   }
93
94   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
95     if (CONFIGURATION_BINDING.equals(clientItfName)) {
96       return configuration;
97     } else if (SELECTION_BINDING.equals(clientItfName)) {
98       return selection;
99     } else if (DISPLAY_BINDING.equals(clientItfName)) {
100       return display;
101     } else if (PRINTER_BINDING.equals(clientItfName)) {
102       return printer;
103     }
104     return null;
105   }
106
107   public void bindFc (
108     final String JavaDoc clientItfName,
109     final Object JavaDoc serverItf)
110   {
111     if (CONFIGURATION_BINDING.equals(clientItfName)) {
112       configuration = (Configuration)serverItf;
113     } else if (SELECTION_BINDING.equals(clientItfName)) {
114       selection = (Selection)serverItf;
115     } else if (DISPLAY_BINDING.equals(clientItfName)) {
116       display = (Display)serverItf;
117     } else if (PRINTER_BINDING.equals(clientItfName)) {
118       printer = (Printer)serverItf;
119     }
120   }
121
122   public void unbindFc (final String JavaDoc clientItfName) {
123     if (CONFIGURATION_BINDING.equals(clientItfName)) {
124       configuration = null;
125     } else if (SELECTION_BINDING.equals(clientItfName)) {
126       selection = null;
127     } else if (DISPLAY_BINDING.equals(clientItfName)) {
128       display = null;
129     } else if (PRINTER_BINDING.equals(clientItfName)) {
130       printer = null;
131     }
132   }
133
134   // -------------------------------------------------------------------------
135
// Implementation of the ActionListener interface
136
// -------------------------------------------------------------------------
137

138   public void actionPerformed (final ActionEvent JavaDoc e) {
139     Object JavaDoc o = selection.getSelection();
140     if (o instanceof Component) {
141       Component c = (Component)o;
142       printer.setType(Printer.TYPE_PRINT);
143
144       List JavaDoc subComponents = c.getSubComponents();
145       if (subComponents.size() > 0) {
146         Object JavaDoc[] options = {"Yes", "No" };
147         int n = JOptionPane.showOptionDialog(
148           null,
149           "Do you want to print all components of the current configuration ?",
150           "Warning",
151           JOptionPane.YES_NO_CANCEL_OPTION,
152           JOptionPane.QUESTION_MESSAGE,
153           null,
154           options,
155           options[0]);
156         if (n == 0) {
157           imprime (c.getRootComponent(), 0);
158           configuration.setRootComponent(c.getRootComponent());
159           selection.selectComponent(c.getRootComponent());
160         }
161         else printer.print(true, c);
162       }
163     }
164   }
165
166   private void imprime (final Component c, final int lvl) {
167
168     boolean on_continue = true;
169     if (lvl == 0) on_continue = printer.print(true, c);
170     else on_continue = printer.print(false, c);
171     if (!on_continue) return;
172
173     List JavaDoc subComponents = c.getSubComponents();
174     for (int i = 0; i < subComponents.size(); ++i) {
175       Component subC = (Component)subComponents.get(i);
176       imprime (subC, lvl+1);
177     }
178   }
179 }
180
Popular Tags