/* * Moonlight|3D Copyright (C) 2007 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package ml.core.document; import java.util.ArrayList; import java.util.List; import ml.core.exceptions.Exception; public class DocumentManager { private ArrayList listeners; private ArrayList documentFactories; private ArrayList openDocuments; private Document mainDocument; public DocumentManager() { listeners=new ArrayList(); documentFactories=new ArrayList(); openDocuments=new ArrayList(); } public void registerDocumentFactory(DocumentFactory factory) { documentFactories.add(factory); } public void unregisterDocumentFactory(DocumentFactory factory) { documentFactories.remove(factory); } public List getDocumentFactories() { return (ArrayList) documentFactories.clone(); } public DocumentFactory getDocumentFactory(String type) { for(DocumentFactory factory : documentFactories) { if(factory.getTypeName().equals(type)) { return factory; } } return null; } public Document createNewMainDocument(String type) { // close current main document if neccessary closeMainDocument(); // create new main document if possible for(DocumentFactory factory : documentFactories) { if(factory.getTypeName().equals(type)) { triggerBeforeNew(); mainDocument=factory.createNewDocument(); triggerAfterNew(); mainDocument.update(); return mainDocument; } } return null; } public Document openMainDocument(String filename) throws Exception { closeMainDocument(); DocumentFactory documentFactory=null; for(DocumentFactory factory : documentFactories) { if(factory.getFileType().matches(filename)) { if(factory.canOpenDocument(filename)) { documentFactory=factory; } } } if(documentFactory==null) { for(DocumentFactory factory : documentFactories) { if(factory.canOpenDocument(filename)) { documentFactory=factory; } } } if(documentFactory==null) { return null; } triggerBeforeOpen(filename); mainDocument=documentFactory.openDocument(filename); triggerAfterOpen(filename); mainDocument.update(); return mainDocument; } public void saveMainDocument() throws Exception { if(mainDocument!=null) { if(mainDocument.getFilename()!=null) { triggerBeforeSave(mainDocument.getFilename()); mainDocument.save(mainDocument.getFilename()); triggerAfterSave(mainDocument.getFilename()); } } } public void saveMainDocument(String filename) throws Exception { if(mainDocument!=null) { triggerBeforeSave(filename); mainDocument.save(filename); triggerAfterSave(filename); } } public void closeMainDocument() { if(mainDocument!=null) { triggerOnClose(); mainDocument.close(); mainDocument=null; } } public Document getMainDocument() { return mainDocument; } public Document createNewDocument(String type) { for(DocumentFactory factory : documentFactories) { if(factory.getTypeName().equals(type)) { Document document=factory.createNewDocument(); openDocuments.add(document); } } return null; } public Document openDocument(String filename) throws Exception { DocumentFactory documentFactory=null; for(DocumentFactory factory : documentFactories) { if(factory.getFileType().matches(filename)) { if(factory.canOpenDocument(filename)) { documentFactory=factory; } } } if(documentFactory==null) { for(DocumentFactory factory : documentFactories) { if(factory.canOpenDocument(filename)) { documentFactory=factory; } } } if(documentFactory==null) { return null; } Document document=documentFactory.openDocument(filename); openDocuments.add(document); return document; } public void saveDocument(Document document) throws Exception { if(document.getFilename()!=null) { document.save(document.getFilename()); } } public void save(Document document, String filename) throws Exception { if(document.getFilename()!=null) { document.save(filename); } } public void closeDocument(Document document) { document.close(); openDocuments.remove(document); } public List getOpenDocuments() { return (ArrayList) openDocuments.clone(); } public void addLifecycleListener(LifecycleListener listener) { listeners.add(listener); } public void removeLifecycleListener(LifecycleListener listener) { listeners.remove(listener); } private void triggerBeforeNew() { for(LifecycleListener listener : listeners) { listener.beforeNew(); } } private void triggerAfterNew() { for(LifecycleListener listener : listeners) { listener.afterNew(); } } private void triggerBeforeOpen(String filename) { for(LifecycleListener listener : listeners) { listener.beforeOpen(filename); } } private void triggerAfterOpen(String filename) { for(LifecycleListener listener : listeners) { listener.afterOpen(filename); } } private void triggerBeforeSave(String filename) { for(LifecycleListener listener : listeners) { listener.beforeSave(filename); } } private void triggerAfterSave(String filename) { for(LifecycleListener listener : listeners) { listener.afterSave(filename); } } private void triggerOnClose() { for(LifecycleListener listener : listeners) { listener.onClose(); } } }