1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.gems;
|
---|
38 |
|
---|
39 | import org.w3c.dom.Document;
|
---|
40 | import org.w3c.dom.Element;
|
---|
41 | import org.greenstone.gatherer.util.XMLTools;
|
---|
42 |
|
---|
43 |
|
---|
44 | import java.util.Observable;
|
---|
45 | import java.util.ArrayList;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
49 | * @version 2.4
|
---|
50 | */
|
---|
51 | public class MetadataSetModel
|
---|
52 | extends Observable implements MetadataSetListener, AttributeListener{
|
---|
53 |
|
---|
54 | private Document metadata_doc;
|
---|
55 |
|
---|
56 | private MetadataSetManager meta_manager;
|
---|
57 |
|
---|
58 | private ArrayList model;
|
---|
59 |
|
---|
60 | private MetadataSetInfo meta_info;
|
---|
61 |
|
---|
62 | private boolean changed = false;
|
---|
63 |
|
---|
64 |
|
---|
65 | //construt a new MetadataSet
|
---|
66 | public MetadataSetModel(MetadataSetManager msm){
|
---|
67 | meta_manager = msm;
|
---|
68 | model = new ArrayList();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public String getCurrentLanguage(){
|
---|
72 | return meta_manager.getCurrentLanguage();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void metadataSetChanged(MetadataSetEvent mse){
|
---|
76 | if (meta_info !=null) save(true); //save the last metadata set
|
---|
77 |
|
---|
78 | model = new ArrayList();
|
---|
79 | meta_info = mse.getMetadataSetInfo();
|
---|
80 | meta_info.setMetadataSetModel(this);
|
---|
81 | metadata_doc = meta_manager.getMetadataSetDocument(meta_info.getFilePath());
|
---|
82 |
|
---|
83 | constructModel();
|
---|
84 |
|
---|
85 | setChanged();
|
---|
86 | notifyObservers();
|
---|
87 | clearChanged();
|
---|
88 | changed = false;
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void valueChanged(){
|
---|
93 | setChanged();
|
---|
94 | notifyObservers(new Boolean(false));
|
---|
95 | clearChanged();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | private void constructModel(){
|
---|
100 | if (metadata_doc == null) return;
|
---|
101 | ArrayList elements =XMLTools.getChildElementsByTagName(metadata_doc.getDocumentElement(),GEMSConstants.ELEMENT_ELEMENT);
|
---|
102 | for(int i=0;i<elements.size();i++){
|
---|
103 | Element element = (Element)elements.get(i);
|
---|
104 | model.add(new MetadataElementModel(element,meta_info));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | public Document getMetadataSetDocument(){
|
---|
110 | return metadata_doc;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void setMetadataSetDocument(Document doc){
|
---|
114 | metadata_doc = doc;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public MetadataSetManager getMetadataSetManager(){
|
---|
118 | return meta_manager;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public ArrayList getMetadataSetModel(){
|
---|
122 | return model;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | public void addChild(MetadataElementModel element_model){
|
---|
127 | model.add(element_model);
|
---|
128 | changed = true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void removeChild(MetadataElementModel element_model){
|
---|
132 | model.remove(element_model);
|
---|
133 | changed = true;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | public void attributeChanged(AttributeEvent ae){
|
---|
138 | changed = true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void setChanged(boolean ischanged){
|
---|
142 | changed = ischanged;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void save(boolean confirm){
|
---|
146 | if ( meta_info == null || (!changed && !meta_info.isNew())) return;
|
---|
147 |
|
---|
148 | meta_info.save();
|
---|
149 |
|
---|
150 | if (metadata_doc !=null){
|
---|
151 | //remove all element elements
|
---|
152 | Element doc_element = metadata_doc.getDocumentElement();
|
---|
153 | ArrayList elements = XMLTools.getChildElementsByTagName(doc_element,GEMSConstants.ELEMENT_ELEMENT);
|
---|
154 | for(int i=0;i< elements.size();i++){
|
---|
155 | Element element = (Element)elements.get(i);
|
---|
156 | doc_element.removeChild(element);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | for(int i=0;i< model.size();i++){
|
---|
161 | MetadataElementModel element_model = (MetadataElementModel)model.get(i);
|
---|
162 | element_model.save();
|
---|
163 | }
|
---|
164 |
|
---|
165 | meta_manager.save(confirm);
|
---|
166 | changed = false;
|
---|
167 |
|
---|
168 | }
|
---|
169 |
|
---|
170 | public MetadataSetInfo getMetadataSetInfo(){
|
---|
171 | return meta_info;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void metadataSetDeleted(){
|
---|
175 | meta_info = null;
|
---|
176 | setChanged();
|
---|
177 | notifyObservers();
|
---|
178 | clearChanged();
|
---|
179 | changed = false;
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | public boolean moveUp(MetadataElementModel child){
|
---|
185 | int index = model.indexOf(child);
|
---|
186 | if (index-1 >=0){
|
---|
187 | changed = true;
|
---|
188 | model.remove(child);
|
---|
189 | model.add(index-1,child);
|
---|
190 | return true;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return false;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public boolean moveDown(MetadataElementModel child){
|
---|
197 | int index = model.indexOf(child);
|
---|
198 | if (index+1 < model.size()){
|
---|
199 | changed = true;
|
---|
200 | model.remove(child);
|
---|
201 | model.add(index+1,child);
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | return false;
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|