001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2004 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package weblog;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import java.util.*;
023:
024: import javax.swing.*;
025:
026: import net.ar.webonswing.*;
027: import net.ar.webonswing.swing.components.*;
028: import net.ar.webonswing.swing.layouts.*;
029:
030: import org.apache.commons.logging.*;
031: import org.wafer.weblog.om.*;
032: import org.wafer.weblog.servlet.*;
033:
034: public class ApplicationFrame extends JDialog {
035: public static final String ACTIVE_USER = "activeUser";
036: public static final String LOOKANDFEEL = "lookandfeel";
037: public static final String WEBLOGSYSTEM = "weblogsystem";
038:
039: protected boolean loginRequired;
040: protected JComponent header;
041: protected JComponent body;
042: protected JComponent menu;
043: protected JComponent footer;
044: protected User activeUser;
045:
046: public ApplicationFrame() {
047: loginRequired = false;
048:
049: if (WosFramework.getSessionContext().get(WEBLOGSYSTEM) == null
050: || WebLogSystem.getUserStore() == null)
051: try {
052: Properties p = new Properties();
053: p.load(Home.class
054: .getResourceAsStream("/weblog.properties"));
055: WebLogSystem.init(p);
056: WosFramework.getSessionContext().put(WEBLOGSYSTEM,
057: "init");
058: WosFramework.getSessionContext().put(LOOKANDFEEL,
059: "flat");
060: } catch (Exception e) {
061: LogFactory.getLog(ApplicationFrame.class).error(
062: "Cannot init ApplicationFrame", e);
063: }
064: }
065:
066: public void initialize() {
067: activeUser = (User) WosFramework.getSessionContext().get(
068: ACTIVE_USER);
069:
070: initBody();
071:
072: header = createHeader();
073: menu = createMenu();
074: footer = createFooter();
075:
076: JPanel panel = (JPanel) getContentPane();
077:
078: panel.setLayout(new BorderLayout());
079:
080: panel.removeAll();
081:
082: panel.add(header, BorderLayout.NORTH);
083: panel.add(menu, BorderLayout.WEST);
084: panel.add(body, BorderLayout.CENTER);
085: panel.add(footer, BorderLayout.SOUTH);
086:
087: String look = WosFramework.getSessionContext().get(LOOKANDFEEL)
088: .equals("flat") ? "ApplicationFrame" : "Aqua";
089:
090: if (WosFramework.isActive())
091: getContentPane().setLayout(
092: new PropagateTemplateLayoutByName(WosFramework
093: .getKeyPositionTemplateForName(look)));
094: else
095: pack();
096: }
097:
098: public void initBody() {
099: if (!loginRequired || activeUser != null)
100: body = createBody();
101: else
102: body = Login.getLoginPanel();
103:
104: body.setPreferredSize(new Dimension(640, 300));
105: }
106:
107: protected JComponent createBody() {
108: return new JPanel();
109: }
110:
111: protected JComponent createFooter() {
112: JPanel newFooter = new JPanel();
113: newFooter.setName("footer");
114:
115: JLabel by = new JLabel("Fernando Damian Petrola");
116: by.setName("by");
117: newFooter.add(by);
118: newFooter.setBackground(new Color(0xFF, 0xFF, 0xEE));
119:
120: return newFooter;
121: }
122:
123: protected JComponent createMenu() {
124: JPanel newMenu = new JPanel(new BorderLayout());
125: newMenu.setName("menu");
126:
127: JLink homeLink = new JLink("Home", Home.class);
128: homeLink.setName("home");
129:
130: JLink aquaLink = new JLink("Aqua Look&Feel", "");
131: aquaLink.setName("aqua");
132: aquaLink.addMouseListener(new MouseAdapter() {
133: public void mouseClicked(MouseEvent e) {
134: WosFramework.getSessionContext().put(LOOKANDFEEL,
135: "aqua");
136: initialize();
137: }
138: });
139:
140: JLink flatLink = new JLink("Flat Look&Feel", "");
141: flatLink.setName("flat");
142: flatLink.addMouseListener(new MouseAdapter() {
143: public void mouseClicked(MouseEvent e) {
144: WosFramework.getSessionContext().put(LOOKANDFEEL,
145: "flat");
146: initialize();
147: }
148: });
149:
150: newMenu.add(homeLink, BorderLayout.NORTH);
151: newMenu.add(aquaLink, BorderLayout.CENTER);
152: newMenu.add(flatLink, BorderLayout.SOUTH);
153: newMenu.setBackground(new Color(0xF8, 0xFF, 0xEE));
154:
155: return newMenu;
156: }
157:
158: protected JComponent createHeader() {
159: JComponent log = null;
160:
161: if (activeUser == null)
162: log = new JLink("Login", Login.class);
163: else {
164: log = new JLink("Logout", "");
165: log.addMouseListener(new MouseAdapter() {
166: public void mouseClicked(MouseEvent arg0) {
167: activeUser = null;
168: WosFramework.getSessionContext()
169: .remove(ACTIVE_USER);
170: WosFramework.showAndExecute(ApplicationFrame.this ,
171: new Home(), null);
172: WosFramework.hide(ApplicationFrame.this );
173: }
174: });
175: }
176:
177: log.setName("login");
178:
179: JLabel image = new JLabel();
180: image.setName("image");
181: image.setText("");
182: image.setIcon(new ImageIcon(
183: (WosFramework.isActive() ? "WebOnSwingWeblog/" : "")
184: + "resources/images/title.gif"));
185: image.setPreferredSize(new Dimension(0, 60));
186: image.setHorizontalAlignment(SwingConstants.CENTER);
187:
188: JLabel help = new JLabel("Help");
189: help.setName("help");
190: help.setPreferredSize(new Dimension(170, 16));
191: help.setVerticalTextPosition(SwingConstants.CENTER);
192: help.setVerticalAlignment(SwingConstants.CENTER);
193: help.setHorizontalAlignment(SwingConstants.CENTER);
194:
195: JLabel username = new JLabel(activeUser != null ? "Hello "
196: + activeUser.getUsername() + "!" : "");
197: username.setName("user");
198: username.setHorizontalAlignment(SwingConstants.CENTER);
199:
200: JPanel panel = new JPanel();
201:
202: panel.setLayout(new BorderLayout());
203:
204: panel.add(image, BorderLayout.NORTH);
205: panel.add(log, BorderLayout.WEST);
206: panel.add(username, BorderLayout.CENTER);
207: panel.add(help, BorderLayout.EAST);
208:
209: panel.setSize(354, 144);
210: panel.setName("header");
211: panel.setBackground(Color.white);
212:
213: return panel;
214: }
215:
216: public User getActiveUser() {
217: return activeUser;
218: }
219:
220: public void setActiveUser(User aUser) {
221: activeUser = aUser;
222: }
223:
224: }
|