View Javadoc
1   /**
2    * 
3    * Copyright (c) 2013-2014, Openflexo
4    * Copyright (c) 2011-2012, AgileBirds
5    * 
6    * This file is part of Gina-swing-editor, a component of the software infrastructure 
7    * developed at Openflexo.
8    * 
9    * 
10   * Openflexo is dual-licensed under the European Union Public License (EUPL, either 
11   * version 1.1 of the License, or any later version ), which is available at 
12   * https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
13   * and the GNU General Public License (GPL, either version 3 of the License, or any 
14   * later version), which is available at http://www.gnu.org/licenses/gpl.html .
15   * 
16   * You can redistribute it and/or modify under the terms of either of these licenses
17   * 
18   * If you choose to redistribute it and/or modify under the terms of the GNU GPL, you
19   * must include the following additional permission.
20   *
21   *          Additional permission under GNU GPL version 3 section 7
22   *
23   *          If you modify this Program, or any covered work, by linking or 
24   *          combining it with software containing parts covered by the terms 
25   *          of EPL 1.0, the licensors of this Program grant you additional permission
26   *          to convey the resulting work. * 
27   * 
28   * This software is distributed in the hope that it will be useful, but WITHOUT ANY 
29   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
30   * PARTICULAR PURPOSE. 
31   *
32   * See http://www.openflexo.org/license.html for details.
33   * 
34   * 
35   * Please contact Openflexo (openflexo-contacts@openflexo.org)
36   * or visit www.openflexo.org if you need additional information.
37   * 
38   */
39  
40  package org.openflexo.gina.swing.editor.view.container;
41  
42  import java.awt.Dimension;
43  import java.awt.event.MouseAdapter;
44  import java.awt.event.MouseEvent;
45  import java.util.Collections;
46  import java.util.List;
47  import java.util.logging.Logger;
48  
49  import javax.swing.DefaultSingleSelectionModel;
50  import javax.swing.JComponent;
51  import javax.swing.JPanel;
52  import javax.swing.JTabbedPane;
53  
54  import org.openflexo.gina.model.container.FIBPanel.Layout;
55  import org.openflexo.gina.model.container.FIBTab;
56  import org.openflexo.gina.model.container.FIBTabPanel;
57  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
58  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerView;
59  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerViewDelegate;
60  import org.openflexo.gina.swing.editor.view.FIBSwingEditableView;
61  import org.openflexo.gina.swing.editor.view.OperatorDecorator;
62  import org.openflexo.gina.swing.editor.view.PlaceHolder;
63  import org.openflexo.gina.swing.view.container.JFIBTabPanelView;
64  import org.openflexo.logging.FlexoLogger;
65  
66  public class JFIBEditableTabPanelView extends JFIBTabPanelView implements FIBSwingEditableContainerView<FIBTabPanel, JTabbedPane> {
67  
68  	private static final Logger logger = FlexoLogger.getLogger(JFIBEditableTabPanelView.class.getPackage().getName());
69  
70  	private final FIBSwingEditableContainerViewDelegate<FIBTabPanel, JTabbedPane> delegate;
71  
72  	private final FIBEditorController editorController;
73  
74  	@Override
75  	public JComponent getDraggableComponent() {
76  		return getTechnologyComponent();
77  	}
78  
79  	@Override
80  	public FIBEditorController getEditorController() {
81  		return editorController;
82  	}
83  
84  	public JFIBEditableTabPanelView(FIBTabPanel model, FIBEditorController editorController) {
85  		super(model, editorController.getController());
86  		this.editorController = editorController;
87  		delegate = new FIBSwingEditableContainerViewDelegate<>(this);
88  	}
89  
90  	@Override
91  	public void delete() {
92  		delegate.delete();
93  		super.delete();
94  	}
95  
96  	@Override
97  	public FIBSwingEditableContainerViewDelegate<FIBTabPanel, JTabbedPane> getDelegate() {
98  		return delegate;
99  	}
100 
101 	@Override
102 	public JTabbedPane getJComponent() {
103 		return super.getJComponent();
104 	}
105 
106 	@Override
107 	public void addSubComponentsAndDoLayout() {
108 		super.addSubComponentsAndDoLayout();
109 		getJComponent().addTab("+", new JPanel());
110 		getJComponent().setModel(new DefaultSingleSelectionModel() {
111 			@Override
112 			public void setSelectedIndex(int index) {
113 				if (getJComponent().getTabCount() > 1) {
114 					index = Math.min(index, getJComponent().getTabCount() - 2);
115 				}
116 				super.setSelectedIndex(index);
117 			}
118 		});
119 		getJComponent().addMouseListener(new MouseAdapter() {
120 			@Override
121 			public void mousePressed(MouseEvent e) {
122 				int tab = getJComponent().getUI().tabForCoordinate(getJComponent(), e.getX(), e.getY());
123 				if (tab == getJComponent().getTabCount() - 1) {
124 					FIBTab newTabComponent = editorController.getFactory().newFIBTab();
125 					newTabComponent.setLayout(Layout.border);
126 					newTabComponent.setTitle("NewTab");
127 					newTabComponent.finalizeDeserialization();
128 					getComponent().addToSubComponents(newTabComponent);
129 				}
130 			}
131 		});
132 	}
133 
134 	@Override
135 	public List<PlaceHolder> makePlaceHolders(Dimension preferredSize) {
136 		return Collections.emptyList();
137 	}
138 
139 	@Override
140 	public List<OperatorDecorator> makeOperatorDecorators() {
141 		return Collections.emptyList();
142 	}
143 
144 	private boolean operatorContentsStart = false;
145 
146 	// TODO: avoid code duplication in FIBSwingEditableView
147 	@Override
148 	public boolean isOperatorContentsStart() {
149 		return operatorContentsStart;
150 	}
151 
152 	// TODO: avoid code duplication in FIBSwingEditableView
153 	@Override
154 	public void setOperatorContentsStart(boolean operatorContentsStart) {
155 		if (operatorContentsStart != this.operatorContentsStart) {
156 			this.operatorContentsStart = operatorContentsStart;
157 			FIBSwingEditableView.updateOperatorContentsStart(this, operatorContentsStart);
158 			getPropertyChangeSupport().firePropertyChange("operatorContentsStart", !operatorContentsStart, operatorContentsStart);
159 		}
160 	}
161 
162 }