001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful,
011      but WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013      GNU Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public License
016      along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
018    
019    package org.objectweb.jac.aspects.gui.web;
020    
021    import java.io.IOException;
022    import java.io.PrintWriter;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Map;
026    import java.util.Vector;
027    import org.apache.log4j.Logger;
028    import org.objectweb.jac.aspects.gui.*;
029    import org.objectweb.jac.aspects.gui.Menu;
030    
031    public class Customized extends AbstractCompositeView 
032        implements CustomizedView, HTMLViewer, WindowListener
033    {
034        static Logger logger = Logger.getLogger("web");
035        static Logger loggerContext = Logger.getLogger("display.context");
036        static Logger loggerEditor = Logger.getLogger("gui.editor");
037    
038        CustomizedGUI customized;
039        //   JPanel contentPanel;
040        PanelView mainView;
041    
042        /**
043         * Builds a customized view.
044         * @param factory the view factory
045         * @param context the display context
046         * @param customized the customized GUI to build a view of
047         * @param panels if not null, overrides the content of the view
048         * (panelID -> PanelContent) 
049         */
050        public Customized(ViewFactory factory, DisplayContext context,
051                          CustomizedGUI customized, Map panels) {
052            this.factory = factory;
053            this.customized = customized;
054            this.context = context;
055            context.setCustomizedView(this);
056    
057            logger.debug("building cutomized...");
058            try {
059                mainView = (PanelView)factory.createCompositeView(
060                    "main",
061                    "Panel",
062                    new Object[] {
063                        new Integer(customized.getSubPanesCount()),
064                        new Integer(customized.getGeometry()),
065                        customized.getPaneContainers(),
066                        customized.getScrollings(),
067                        customized.getSplitters() },
068                    context
069                );
070            } catch (ViewFactory.UnhandledViewTypeException e) {
071                e.printStackTrace();
072            }
073    
074            //      setPosition(customized.getLeft(),customized.getUp(),
075            //                  customized.getWidth(),customized.getHeight());
076            try {
077                GenericFactory.initCustomized(factory, context, mainView, 
078                                              customized, panels);
079                if (customized.hasMenuBar())
080                    GenericFactory.setMenuBars(factory, context, 
081                                               this, customized.getMenus());
082                if (customized.hasToolBar())
083                    GenericFactory.setToolBar(factory, context, 
084                                              this, customized.getToolbar());
085                if (customized.hasStatusBar())
086                    GenericFactory.setStatusBar(factory, context, 
087                                                this, customized.getStatusBarMethod(), 
088                                                customized.getStatusPosition());
089    
090            } catch (Exception e) {
091                logger.error("Customized("+customized+")",e);
092            }
093            logger.debug("building cutomized DONE");
094        }
095    
096        public void close(boolean validate) {
097            mainView.close(validate);
098        }   
099    
100        // View interface
101    
102        public void setContext(DisplayContext context) {
103            super.setContext(context);
104            loggerContext.debug("setContext on "+getClass().getName());
105            // recursively set the display of inner components
106            Iterator i = mainView.getViews().iterator();
107            while (i.hasNext()) {
108                View view = (View)i.next();
109                loggerContext.debug("set context on subView "+view);
110                view.setContext(context);
111            }
112        }
113    
114        public void addView(View view, Object extraInfos) {
115            view.setContext(context);
116            mainView.addView(view,extraInfos);
117        }
118    
119        public void addView(View view) {
120            addView(view,null);
121        }
122    
123        public Collection getViews() {
124            return mainView.getViews();
125        }
126    
127        public View getView(Object id) {
128            return mainView.getView(id);
129        }
130    
131        public void removeAllViews(boolean validate) {
132            mainView.removeAllViews(validate);
133        }
134    
135        // CustomizedView interface
136    
137        public CustomizedGUI getCustomizedGUI() {
138            return customized;
139        }
140    
141        Vector menuBars = new Vector();
142        MenuView topMenuBar;
143        MenuView bottomMenuBar;
144        MenuView leftMenuBar;
145        MenuView rightMenuBar;
146    
147        public void setMenuBar(MenuView menuBar,String position) {
148            if (position==null)
149                position = Menu.LEFT;
150            menuBar.setPosition(position);
151            if (position.equals(Menu.TOP)) {
152                topMenuBar=menuBar;
153            } else if(position.equals(Menu.BOTTOM)) {
154                bottomMenuBar=menuBar;
155            } else if(position.equals(Menu.LEFT)) {
156                leftMenuBar=menuBar;
157            } else if(position.equals(Menu.RIGHT)) {
158                rightMenuBar=menuBar;
159            }
160            this.menuBars.add(menuBar);
161        }
162    
163        public MenuView getTopMenuBar() {
164            return topMenuBar;
165        }
166    
167        public MenuView getBottomMenuBar() {
168            return bottomMenuBar;
169        }
170    
171        public MenuView getLeftMenuBar() {
172            return leftMenuBar;
173        }
174    
175        public MenuView getRightMenuBar() {
176            return rightMenuBar;
177        }
178    
179        MenuView toolBar;
180        public void setToolBar(MenuView toolBar) {
181            this.toolBar = toolBar;
182        }
183    
184        StatusView statusBar;
185        public void setStatusBar(StatusView statusBar,String position) {
186            this.statusBar=statusBar;
187            statusBar.setPosition(position);
188        }
189    
190        public void showStatus(String message) {
191            statusBar.showMessage(message);
192        }
193    
194        public PanelView getPanelView() {
195            return mainView;
196        }
197    
198        public void requestFocus() {
199        }
200    
201        // HTMLViewer interface
202       
203        public void genHTML(PrintWriter out) throws IOException {
204            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
205            out.println("<html>");
206            out.println("  <head>");
207            out.println("  <title>"+label+"</title>");
208            out.println("  <meta name=\"Author\" content=\"JAC web-gui server\">" );
209            out.println("  <script type=\"text/javascript\" src=\""+javascript+"\"></script>");
210            if (customized.getIcon()!=null) {
211                String resource = ResourceManager.getResource(customized.getIcon());
212                if (resource!=null)
213                    out.println("  <link rel=\"icon\" href=\"resources/"+resource+"\" />");
214            }
215            genStyleSheets(out,context.getCustomizedView());
216            out.println("  </head>");
217            out.println("  <body>");
218            genBody(out);
219            out.println("  </body>");
220            out.println("</html>");
221        }
222    
223        protected void genBody(PrintWriter out) throws IOException {
224            openForm(out);
225            out.println("<table class=\"customized\">");
226            if (statusBar!=null && statusBar.getPosition().equals(CustomizedGUI.TOP)) {
227                out.println("  <tr>\n    <td class=\"statusBar\" colspan=\"3\">");         
228                ((HTMLViewer)statusBar).genHTML(out);
229                out.println("    </td>\n  </tr>");
230            }
231            if (topMenuBar!=null) {
232                out.println("  <tr>\n    <td class=\"menuBarT\" colspan=\"3\">");
233                ((HTMLViewer)topMenuBar).genHTML(out);
234                if (toolBar!=null) {
235                    ((HTMLViewer)toolBar).genHTML(out);
236                }
237                out.println("    </td>\n  </tr>");
238            }
239            out.println("  <tr>");
240            int colspan = 1;
241            if (leftMenuBar==null)
242                colspan++;
243            if (rightMenuBar==null)
244                colspan++;
245            if (leftMenuBar!=null) {
246                out.println("    <td class=\"menuBarL\">");
247                ((HTMLViewer)leftMenuBar).genHTML(out);
248                out.println("    </td>");
249            }
250            out.println("    <td"+(colspan>1?(" colspan=\""+colspan+"\""):"")+" class=\"mainView\"><div class=\"mainView\">");
251            //      out.println("  ");
252            ((HTMLViewer)mainView).genHTML(out);
253            out.println("  \n</div>\n    </td>");
254            if (rightMenuBar!=null) {
255                out.println("    <td class=\"menuBarR\">");
256                ((HTMLViewer)rightMenuBar).genHTML(out);
257                out.println("    </td>");
258            }
259            out.println("  </tr>");
260            if (bottomMenuBar!=null) {
261                out.println("  <tr>\n    <td class=\"menuBarB\" colspan=\"3\">");
262                ((HTMLViewer)bottomMenuBar).genHTML(out);
263                out.println("    </td>\n  </tr>");
264            }
265            if (statusBar!=null && statusBar.getPosition().equals(CustomizedGUI.BOTTOM)) {
266                out.println("  <tr>\n    <td class=\"statusBar\" colspan=\"3\">");         
267                ((HTMLViewer)statusBar).genHTML(out);
268                out.println("    </td>\n  </tr>");
269            }
270            out.println("</table>");
271    
272            if (context.hasEnabledEditor()) {
273                out.println("<div id=\"buttons\">");
274                loggerEditor.debug("editors = "+context.getEditors());
275                showButton(out,null,GuiAC.getLabelOK(),"onOK");
276                showButton(out,null,GuiAC.getLabelCancel(),"onCancel");
277                out.println("</div>");
278            }
279            
280            closeForm(out);
281        }
282    
283        // WindowListener interface
284    
285        public void onOK(JacRequest request) {
286            WebDisplay display = (WebDisplay)context.getDisplay();
287            WebDisplay.readValuesAndRefresh(context,request,true);
288        }
289    
290        public void onRefresh(JacRequest request) {
291            WebDisplay display = (WebDisplay)context.getDisplay();
292            WebDisplay.readValuesAndRefresh(context,request,true);
293        }
294    
295        public void onCancel() {
296            ((WebDisplay)context.getDisplay()).refresh();
297        }
298    
299        public void onValidate(JacRequest request) {
300            WebDisplay.readValues(context,request,true);
301        }
302    
303    }
304