View Javadoc
1   package org.openflexo.gina.swing.editor;
2   
3   import java.util.logging.Logger;
4   
5   import org.openflexo.gina.ApplicationFIBLibrary.ApplicationFIBLibraryImpl;
6   import org.openflexo.gina.FIBLibrary;
7   import org.openflexo.gina.model.FIBComponent;
8   import org.openflexo.gina.model.FIBModelFactory;
9   import org.openflexo.logging.FlexoLogger;
10  import org.openflexo.pamela.exceptions.ModelDefinitionException;
11  import org.openflexo.rm.FileResourceImpl;
12  import org.openflexo.rm.Resource;
13  import org.openflexo.toolbox.PropertyChangedSupportDefaultImplementation;
14  
15  /**
16   * This represent the edition of a {@link FIBComponent}, in a given context<br>
17   * 
18   * @author sylvain
19   *
20   */
21  public class EditedFIBComponent extends PropertyChangedSupportDefaultImplementation {
22  
23  	static final Logger logger = FlexoLogger.getLogger(EditedFIBComponent.class.getPackage().getName());
24  
25  	private String name;
26  	private Resource sourceResource;
27  	private Resource productionResource;
28  	private FIBComponent fibComponent;
29  	private final FIBLibrary fibLibrary;
30  	private FIBModelFactory fibFactory;
31  	private Object dataObject;
32  
33  	protected EditedFIBComponent(FIBComponent fibComponent, FIBLibrary fibLibrary) {
34  		super();
35  		this.fibLibrary = fibLibrary;
36  		this.fibComponent = fibComponent;
37  
38  	}
39  
40  	protected EditedFIBComponent(String name, FIBComponent fibComponent, FIBLibrary fibLibrary) {
41  		this(fibComponent, fibLibrary);
42  		this.name = name;
43  	}
44  
45  	protected EditedFIBComponent(Resource sourceResource, FIBLibrary fibLibrary) {
46  		super();
47  		this.fibLibrary = fibLibrary;
48  		load(sourceResource);
49  	}
50  
51  	protected EditedFIBComponent(Resource sourceResource, Resource productionResource, FIBLibrary fibLibrary) {
52  		this(sourceResource, fibLibrary);
53  		this.productionResource = productionResource;
54  	}
55  
56  	public String getName() {
57  		if (name != null) {
58  			return name;
59  		}
60  		if (getSourceResource() instanceof FileResourceImpl) {
61  			return ((FileResourceImpl) getSourceResource()).getFile().getName();
62  		}
63  		return "untitled.fib";
64  	}
65  
66  	public FIBComponent getFIBComponent() {
67  		return fibComponent;
68  	}
69  
70  	public FIBLibrary getFIBLibrary() {
71  		return fibLibrary;
72  	}
73  
74  	public Object getDataObject() {
75  		return dataObject;
76  	}
77  
78  	public void setDataObject(Object dataObject) {
79  		if ((dataObject == null && this.dataObject != null) || (dataObject != null && !dataObject.equals(this.dataObject))) {
80  			Object oldValue = this.dataObject;
81  			this.dataObject = dataObject;
82  			getPropertyChangeSupport().firePropertyChange("dataObject", oldValue, dataObject);
83  		}
84  	}
85  
86  	public FIBModelFactory getFactory() {
87  		if (fibFactory != null) {
88  			return fibFactory;
89  		}
90  		if (getFIBLibrary() != null) {
91  			return getFIBLibrary().getFIBModelFactory();
92  		}
93  		return ApplicationFIBLibraryImpl.instance().getFIBModelFactory();
94  	}
95  
96  	public Resource getSourceResource() {
97  		return sourceResource;
98  	}
99  
100 	public void setSourceResource(Resource sourceResource) {
101 		if ((sourceResource == null && this.sourceResource != null)
102 				|| (sourceResource != null && !sourceResource.equals(this.sourceResource))) {
103 			Resource oldValue = this.sourceResource;
104 			this.sourceResource = sourceResource;
105 			getPropertyChangeSupport().firePropertyChange("sourceResource", oldValue, sourceResource);
106 		}
107 	}
108 
109 	public Resource getProductionResource() {
110 		if (productionResource == null && getSourceResource() != null) {
111 			return getSourceResource();
112 		}
113 		return productionResource;
114 	}
115 
116 	public void setProductionResource(Resource productionResource) {
117 		if ((productionResource == null && this.productionResource != null)
118 				|| (productionResource != null && !productionResource.equals(this.productionResource))) {
119 			Resource oldValue = this.productionResource;
120 			this.productionResource = productionResource;
121 			getPropertyChangeSupport().firePropertyChange("productionResource", oldValue, productionResource);
122 		}
123 	}
124 
125 	private void load(Resource sourceResource) {
126 		setSourceResource(sourceResource);
127 
128 		if (sourceResource != null) {
129 			try {
130 				fibFactory = new FIBModelFactory(sourceResource.getContainer(), null);
131 			} catch (ModelDefinitionException e) {
132 				e.printStackTrace();
133 				return;
134 			}
135 		}
136 		System.out.println("Loading " + sourceResource);
137 		this.fibComponent = fibLibrary.retrieveFIBComponent(sourceResource, true, getFactory());
138 		System.out.println("Done: fibComponent=" + this.fibComponent);
139 	}
140 
141 	public void save() {
142 		if (getSourceResource() != null) {
143 			logger.info("Save to " + getSourceResource());
144 			getFIBLibrary().save(fibComponent, getSourceResource());
145 		}
146 		else {
147 			logger.warning("Cannot save FIBComponent: source resource is null");
148 		}
149 	}
150 
151 	public void saveAs(FileResourceImpl sourceResource) {
152 		String oldName = getName();
153 		name = null;
154 		setSourceResource(sourceResource);
155 		save();
156 		getPropertyChangeSupport().firePropertyChange("name", oldName, getName());
157 	}
158 
159 }