//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataviewer;
import com.nexirius.framework.command.DefaultProcessor;
import com.nexirius.framework.command.Processor;
import com.nexirius.framework.datamodel.DataModelCommand;
import com.nexirius.framework.datamodel.Viewable;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelCommandVector;
import com.nexirius.framework.swing.CFJButton;
import com.nexirius.framework.swing.CFJLabel;
import com.nexirius.util.resource.ClientResource;
import com.nexirius.util.resource.ClientResourceImpl;
import com.nexirius.util.resource.ClientResourceToolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.DateFormat;
public class ViewerFactory {
public static final String OK_BUTTON = "Ok";
public static final String CANCEL_BUTTON = "Cancel";
public static final String CLOSE_BUTTON = "Close";
public static final String HELP_BUTTON = "Help";
ClientResource resources;
Processor processor = null;
ViewerCreatorMap viewerCreatorMap = new BasicViewerCreatorMap(); // maps viewer names to viewer creator class names
static ViewerFactory instance = null;
/**
* returns default instance (or null if not initialized with getDefaultInstance or with setInstance)
*/
public static ViewerFactory getInstance() {
return instance;
}
/**
* set the static member which holds the default viewer factory instance
*
* @param vf
*/
public static void setInstance(ViewerFactory vf) {
instance = vf;
}
/**
* create and initialize default instance
*/
public static ViewerFactory getDefaultInstance(String resourceFileName)
throws Exception {
ClientResource resource = new ClientResourceImpl(resourceFileName);
ClientResourceToolkit.initialise(resource);
instance = new ViewerFactory(resource);
instance.setCommandProcessor(new DefaultProcessor());
instance.setViewerCreatorMap(new BasicViewerCreatorMap());
return instance;
}
public ViewerFactory(ClientResource resources) {
this.resources = resources;
}
public void setViewerCreatorMap(ViewerCreatorMap viewerCreatorMap) {
this.viewerCreatorMap = viewerCreatorMap;
}
public ViewerCreatorMap getViewerCreatorMap() {
return viewerCreatorMap;
}
public String getText(String text) {
String ret = resources.getText(text);
return ret != null ? ret : text;
}
public void setCommandProcessor(Processor processor) {
this.processor = processor;
}
public Processor getCommandProcessor() {
return this.processor;
}
public void callMethod(DataModelCommand method) {
if (processor != null) {
processor.execute(method, null);
} else {
method.doAction();
new Exception("ERROR: have no command processor assigned").printStackTrace();//FIX
}
}
public int getInt(String id)
throws Exception {
return resources.getInt(id);
}
/* public String getText(String text, java.util.Locale locale)
{
return this.textSource != null ? this.textSource.getText(text, locale) : text;
}
*/
public Icon getIcon(String icon_id) {
if (icon_id == null) {
return null;
}
return resources.getIcon(icon_id);
}
public Color getColor(String color_id) {
if (color_id == null) {
return null;
}
return resources.getColor(color_id);
}
public Font getFont(String font_id) {
return resources.getFont(font_id);
}
public Font getFont() {
return resources.getFont(ClientResource.DEFAULT); //FIX
}
public ClientResource getClientResource() {
return resources;
}
public DateFormat getDateFormat() {
return resources.getDateFormat(ClientResource.DEFAULT);
}
public DataViewer createDefaultViewer(Viewable viewable)
throws Exception {
if (viewable == null) {
throw new IllegalArgumentException("Can't create viewer from null");
}
return createViewer(viewable, false);
}
public DataViewer createDefaultEditor(Viewable viewable)
throws Exception {
if (viewable == null) {
throw new IllegalArgumentException("Can't create editor from null");
}
return createViewer(viewable, true);
}
public DataViewer createViewer(Viewable viewable, boolean isEditor)
throws Exception {
if (viewable == null) {
throw new IllegalArgumentException("Can't create viewer from null");
}
ViewerCreator creator = viewerCreatorMap.getViewerCreator(viewable, isEditor);
return createViewer(creator, viewable);
}
public DataViewer createViewer(ViewerCreator creator, Viewable viewable) {
DataViewer ret = creator.createViewer(viewable);
ret.setFactory(this);
return ret;
}
public JLabel createJLabel(String textResourceId) {
return new CFJLabel(resources, textResourceId);
}
public JButton createJButton(String textResourceId, ActionListener actionListener) {
JButton button = new CFJButton(resources, textResourceId);
if (actionListener != null) {
button.addActionListener(actionListener);
}
return button;
}
public void arrangeButtons(JButton buttons[], JPanel panel) {
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Box.createVerticalStrut(6));
Box box = new Box(BoxLayout.X_AXIS);
for (int i = 0; i < buttons.length; ++i) {
box.add(Box.createHorizontalGlue());
box.add(buttons[i]);
}
box.add(Box.createHorizontalGlue());
panel.add(box);
panel.add(Box.createVerticalStrut(6));
}
public JButton[] createButtons(String labels[], JPanel panel) {
JButton ret[] = new JButton[labels.length];
for (int i = 0; i < ret.length; ++i) {
ret[i] = new CFJButton(getText(labels[i]));
}
arrangeButtons(ret, panel);
return ret;
}
public JPopupMenu createPopupMenu(DataModel model) {
JPopupMenu ret = null;
if (model.hasMethods()) {
DataModelCommandVector v = model.getMethods();
ret = new JPopupMenu();
int maxWidth = 200;
for (DataModelCommand m = v.firstItem(); m != null; m = v.nextItem()) {
try {
CommandViewer viewer = (CommandViewer)createDefaultViewer(m);
JMenuItem jMenuItem = viewer.createJMenuItem();
maxWidth = Math.max(jMenuItem.getPreferredSize().width, maxWidth);
ret.add(jMenuItem);
} catch (Exception ex) {
//FIX
ex.printStackTrace();
}
}
ret.setPreferredSize(new Dimension(maxWidth + 40, ret.getPreferredSize().height));
}
return ret;
}
}