//{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.ulc.application;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DefaultDataModelCommand;
import com.nexirius.framework.datamodel.StructModel;
import com.nexirius.ulc.ulcviewer.ULCDialogViewerFactory;
import com.nexirius.ulc.ulcviewer.ULCViewerFactory;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCDialog;
public class ULCDialogManager {
private static ULCDialogManager instance = null;
private ULCApplication application;
private ULCDialogManager(ULCApplication application) {
this.application = application;
}
public static ULCDialogManager instance() {
return instance;
}
public static void init(ULCApplication application) {
if (instance != null) {
throw new RuntimeException("ULCDialogManager is already initialized");
}
instance = new ULCDialogManager(application);
}
public ULCDialog createDialog() {
return new ULCDialog(application.getMainUlcFrame());
}
/**
* Calls popup(model, true)
* @param model
* @return
*/
public ULCDialog popup(DataModel model) {
return popup(model, true);
}
/**
* Creates a dialog window where the given model is diaplayed as an edidtor.
* @param model The model for which a dialog window is opened
* @param duplicate If true then a copy of the model is displayed and not only the OK but also a Cancel button is displayed at the bottom of the winndow
* @return The created dialog window
*/
public ULCDialog popup(DataModel model, boolean duplicate) {
return popup(model, duplicate, null);
}
/**
* Creates a dialog window where the given model is diaplayed as an edidtor.
* @param model The model for which a dialog window is opened
* @param duplicate If true then a copy of the model is displayed and not only the OK but also a Cancel button is displayed at the bottom of the winndow
* @return The created dialog window
*/
public ULCDialog popup(DataModel model, boolean duplicate, ULCDialogListener ulcDialogListener) {
ULCViewerFactory ulcFactory = application.getUlcFactory();
ULCDialogViewerFactory ulcDialogViewerFactory = new ULCDialogViewerFactory(ulcFactory.getClientResource());
ulcDialogViewerFactory.setUlcViewerCreatorMap(ulcFactory.getUlcViewerCreatorMap());
ulcDialogViewerFactory.startDialog();
try {
ULCDialog dialog = createDialog();
DataModel copy = null;
String fieldName = model.getFieldName();
if (fieldName != null) {
dialog.setTitle(ulcFactory.getClientResource().getLabel(fieldName));
}
if (duplicate) {
copy = model.duplicate(null, null);
}
DialogModel dialogModel = new DialogModel(ulcDialogViewerFactory, dialog, model, copy, ulcDialogListener);
ULCComponent editorPanel = ulcDialogViewerFactory.createDefaultUlcEditor(duplicate ? copy : model).getULCComponent(ulcDialogViewerFactory);
ULCComponent buttonPanel = ulcDialogViewerFactory.createDefaultUlcEditor(dialogModel).getULCComponent(ulcDialogViewerFactory);
ULCBoxPane boxPane = new ULCBoxPane(1, 0);
boxPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, editorPanel);
boxPane.add(buttonPanel);
dialog.add(boxPane);
dialog.setVisible(true);
return dialog;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static class DialogModel extends StructModel {
public static final String OK_COMMAND = "okCommand";
public static final String CANCEL_COMMAND = "cancelCommand";
private ULCDialog dialog;
private ULCDialogViewerFactory factory;
private DataModel original;
private DataModel copy;
private ULCDialogListener ulcDialogListener;
public DialogModel(ULCDialogViewerFactory factory, ULCDialog dialog, DataModel original, DataModel copy, ULCDialogListener ulcDialogListener) {
super("DialogModel");
this.factory = factory;
this.dialog = dialog;
this.original = original;
this.copy = copy;
this.ulcDialogListener = ulcDialogListener;
init();
}
private void init() {
appendMethod(new DefaultDataModelCommand(OK_COMMAND));
if (copy != null) {
appendMethod(new DefaultDataModelCommand(CANCEL_COMMAND));
}
}
public void okCommand() {
factory.stopDialog(true);
dialog.setVisible(false);
if (copy != null) {
try {
original.dropData(copy.dragData());
if (ulcDialogListener != null) {
ulcDialogListener.ok(original);
}
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
}
public void cancelCommand() {
factory.stopDialog(false);
dialog.setVisible(false);
if (ulcDialogListener != null) {
ulcDialogListener.cancel(original);
}
}
}
}