//{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.layout;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.ViewerCreator;
import com.nexirius.framework.swing.CFJTabbedPane;
import javax.swing.*;
import java.util.Vector;
public class TabLayoutItem extends StructureFieldLayout {
String tabLabel;
String tabIcon;
Vector tabModelListeners = new Vector(); // just bind these listeners to the controller (so they are not GCed)
public TabLayoutItem() {
super();
}
/**
* @param field The associated structure field name
* @param x the x position of the component
* @param y the y position of the component
* @param w the width of the component (if twidth and theight are both 0 then they are ignored)
* @param h the height of the component (if twidth and theight are both 0 then they are ignored)
* @param viewerCreator the viewer creator which is used to create the component or null (default viewer from the model)
* @param layout the layout which is used to layout the component or null (default layout from the model)
* @param createLabel creates a frame with the label when true
*/
public TabLayoutItem(String field, int x, int y, int w, int h, ViewerCreator viewerCreator, DefaultLayoutItem layout, boolean createLabel, String tabLabel, String tabIcon) {
super(field, x, y, w, h, viewerCreator, layout, createLabel);
this.tabLabel = tabLabel;
this.tabIcon = tabIcon;
}
public void doLayout(DataViewer parent_viewer) {
try {
DataModel parent_model = parent_viewer.getDataModel();
// look for the associated model
Viewable model = parent_model.getViewableChild(getModelFieldName());
DataViewer viewer = null;
CFJTabbedPane tabbedPane = (CFJTabbedPane) parent_viewer.getJComponent();
if (viewerCreator == null) {
viewer = parent_viewer.getFactory().createViewer(model, parent_viewer.isEditor());
} else {
viewer = parent_viewer.getFactory().createViewer(viewerCreator, model);
}
// layout the associated model
viewer.setLayout(layout);
// create a JComponent from the associated viewer
JComponent comp = viewer.getJComponent();
int tabIndex = tabbedPane.indexOfTab(tabLabel);
if (tabIndex < 0) {
// have to create a new tab
tabbedPane.addTab(tabLabel, tabIcon, comp);
tabIndex = tabbedPane.getTabCount() - 1;
if (model instanceof DataModel) {
DataModel dataModel = (DataModel) model;
tabModelListeners.add(new TabModelListener(tabbedPane, tabIndex, dataModel));
}
}
if (createLabel) {
createFieldName(parent_viewer.getFactory(), model.getFieldName(), comp);
}
// move and resize the JComponent
defaultLayout(comp, parent_viewer.getFactory());
} catch (Exception ex) {
ex.printStackTrace();
// ignore
}
}
class TabModelListener extends DataModelAdaptor {
JTabbedPane tabbedPane;
int tabIndex;
DataModel dataModel;
public TabModelListener(JTabbedPane tabbedPane, int tabIndex, DataModel dataModel) {
dataModel.addSoftDataModelListener(this);
this.tabbedPane = tabbedPane;
this.tabIndex = tabIndex;
this.dataModel = dataModel;
update();
}
public void update() {
SwingUtilities.invokeLater(new Updater());
}
public void dataModelChangeValue(DataModelEvent event) {
if (event.getDataModel() == dataModel) {
update();
}
}
public class Updater implements Runnable {
public void run() {
tabbedPane.setEnabledAt(tabIndex, !dataModel.getFlag(ModelFlag.INVISIBLE));
}
}
}
}