//{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.tools.properties.model;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.application.ErrorMessageException;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.swing.JnexFileChooser;
import com.nexirius.util.StringVector;
import com.nexirius.util.XFile;
import com.nexirius.util.XString;
import javax.swing.filechooser.FileFilter;
import java.io.File;
public class MainModel extends StructModel {
SettingsModel settings;
EntryArrayModel entryArray;
BooleanModel needSaving;
StringModel title;
String openFileName;
protected JnexFileChooser chooser;
public static final String COMMAND_NEW = "newCommand";
public static final String COMMAND_OPEN = "openCommand";
public static final String COMMAND_SAVE = "saveCommand";
public static final String COMMAND_SAVE_AS = "saveAsCommand";
public static final String COMMAND_EDIT = "editEntryCommand";
public static final String COMMAND_ADD = "addEntryCommand";
public static final String COMMAND_DELETE = "deleteEntryCommand";
public static final String COMMAND_DUPLICATE = "duplicateEntryCommand";
public static final String COMMAND_EDIT_SETTINGS = "editSettingsCommand";
public static final String PROPERTIES_EXTENSION = ".properties";
public MainModel() {
super("Main");
init();
}
private void init() {
append(title = new StringModel("", "title"));
append(needSaving = new BooleanModel(false, "needSaving"));
appendMethod(new DefaultDataModelCommand(COMMAND_NEW));
appendMethod(new DefaultDataModelCommand(COMMAND_OPEN));
appendMethod(new DefaultDataModelCommand(COMMAND_SAVE));
appendMethod(new DefaultDataModelCommand(COMMAND_SAVE_AS));
appendMethod(new DefaultDataModelCommand(COMMAND_EDIT));
appendMethod(new DefaultDataModelCommand(COMMAND_ADD));
appendMethod(new DefaultDataModelCommand(COMMAND_DELETE));
appendMethod(new DefaultDataModelCommand(COMMAND_DUPLICATE));
appendMethod(new DefaultDataModelCommand(COMMAND_EDIT_SETTINGS));
}
public void initSettings(SettingsModel settings) {
append(this.settings = settings);
if (entryArray != null) {
removeItem(entryArray);
}
append(entryArray = new EntryArrayModel());
entryArray.addDataModelListener(new EntryArrayListener());
}
public void relayout() {
}
public boolean needSaving() {
return needSaving.getBoolean();
}
public String getTitle() {
return title.getText();
}
public void addTitleListener(DataModelListener listener) {
title.addDataModelListener(listener);
needSaving.addDataModelListener(listener);
}
public void newCommand() {
if (needSaving.getBoolean()) {
if (DialogManager.getAskAdaptor().ask("SaveExistingData", "OK", "Cancel", true)) {
saveCommand();
} else {
return;
}
}
entryArray.clear();
title.clear();
openFileName = null;
needSaving.setBoolean(false);
}
public void openCommand() {
initChooser();
File file = chooser.popupOpenDialog(openFileName);
if (file != null) {
openFile(file.getPath());
}
}
private void initChooser() {
if (chooser == null) {
chooser = DialogManager.getFileChooser(null);
chooser.getJFileChooser().setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory() || f.getName().endsWith(".properties");
}
public String getDescription() {
return "*.properties";
}
});
}
}
public void openFile(String fileName) {
if (needSaving.getBoolean()) {
if (DialogManager.ask("SaveBeforeLoad", "yes", "no", true, false)) {
saveCommand();
}
}
entryArray.removeChildren();
try {
entryArray.readEntries(getFilename(fileName, null));
readLocaleEntries(fileName);
} catch (Exception e) {
throw new ErrorMessageException("CannotRead{0}", e, new Object[] {fileName});
}
openFileName = fileName;
needSaving.setBoolean(false);
title.setText(fileName);
}
/**
* get the directory name (contains a File.separator at the end)
*/
public String getDirectory(String fileName) {
// remove the file name part from the fileName (if any)
int sepIndex = fileName.lastIndexOf(File.separatorChar);
if (sepIndex >= 0 && sepIndex < fileName.length() - 1) {
fileName = fileName.substring(0, sepIndex + 1);
}
return fileName;
}
private void readLocaleEntries(String fileName) throws Exception {
// find locale files
XFile directory = new XFile(getDirectory(fileName));
StringVector files = directory.getFiles(true, "*.properties");
String baseFileName = getBaseFileName(fileName);
for (String localeFile = files.firstItem(); localeFile != null; localeFile = files.nextItem()) {
localeFile = getDirectory(fileName) + localeFile;
if (XString.match(baseFileName + "_*", localeFile)) {
String extension = getBaseFileName(localeFile).substring(baseFileName.length() + 1);
parseLocaleFile(localeFile, extension);
}
}
}
private void parseLocaleFile(String localeFile, String locale) throws Exception {
XFile file = new XFile(localeFile);
StringVector lines = file.getTextLines();
for (String line = lines.firstItem(); line != null; line = lines.nextItem()) {
EntryModel newEntry = new EntryModel();
newEntry.parse(line);
entryArray.addLocaleEntry(locale, newEntry);
}
}
private String getBaseFileName(String filename) {
return filename.substring(0, filename.length() - ".properties".length());
}
public String getFilename(String fileName, String locale) {
if (locale == null) {
return fileName;
} else {
return getBaseFileName(fileName) + "_" + locale + ".properties";
}
}
public void addEntryCommand() {
EntryModel entry = new EntryModel();
if (DialogManager.getPopupEditorAdaptor().popupEdit(entry)) {
entryArray.setHighlightedItem(entryArray.sortInsert(entry));
}
}
public void editEntryCommand() {
EntryModel act = (EntryModel) entryArray.getHighlightedItem();
if (act == null) {
DialogManager.warning("NothingHighlighted");
} else {
if (DialogManager.getPopupEditorAdaptor().popupEdit(act)) {
entryArray.sort();
}
}
}
public void duplicateEntryCommand() {
EntryModel act = (EntryModel) entryArray.getHighlightedItem();
if (act == null) {
DialogManager.warning("NothingHighlighted");
} else {
EntryModel newEntry = (EntryModel) act.duplicate(null, null);
if (DialogManager.getPopupEditorAdaptor().popupEdit(newEntry)) {
entryArray.setHighlightedItem(entryArray.sortInsert(newEntry));
}
}
}
public void deleteEntryCommand() {
DataModel act = entryArray.getHighlightedItem();
if (act == null) {
DialogManager.warning("NothingHighlighted");
} else {
if (DialogManager.ask("ReallyDelete", "Delete", "Cancel", true)) {
entryArray.removeItem(act);
entryArray.setHighlightedItem(-1);
}
}
}
public void saveAsCommand() {
saveFileAs(null);
}
public void saveCommand() {
saveFileAs(openFileName);
}
public void saveFileAs(String fileName) {
if (fileName == null) {
initChooser();
File file = chooser.popupSaveDialog(null);
if (file == null) {
return;
}
fileName = file.getPath();
}
if (!fileName.endsWith(PROPERTIES_EXTENSION)) {
fileName = fileName + PROPERTIES_EXTENSION;
}
if (!fileName.equals(openFileName)) {
if (new File(fileName).exists()) {
if (!DialogManager.ask("Overwrite exisiting file: " + fileName, "OK", "Cancel", true)) {
return;
}
}
}
writeFile(fileName, new XFile(fileName), null);
DataModelEnumeration en = settings.getLocaleArrayModel().getEnumeration();
while (en.hasMore()) {
LocaleModel lm = (LocaleModel) en.next();
String locale = lm.getFileExtension();
XFile file = new XFile(getFilename(fileName, locale));
writeFile(fileName, file, locale);
}
openFileName = fileName;
needSaving.setBoolean(false);
title.setText(fileName);
}
private void writeFile(String fileName, XFile file, String locale) {
if (!file.exists() || file.canWrite()) {
try {
entryArray.saveTo(getFilename(fileName, locale), locale);
needSaving.setBoolean(false);
} catch (Exception ex) {
throw new ErrorMessageException("CannotWrite{0}", ex, new String[]{file.toString()}) ;
}
} else {
throw new ErrorMessageException("CannotWrite{0}", null, new String[]{file.toString()}) ;
}
}
public SettingsModel getSettings() {
return settings;
}
public boolean exit() {
if (needSaving.getBoolean()) {
if (DialogManager.ask("SaveBeforeExit", "yes", "no", true, false)) {
saveCommand();
return false;
}
}
return true;
}
public void editSettingsCommand() {
DialogManager.getPopupEditorAdaptor().noDuplicatePopupEdit(settings, null, null);
}
public String getOpenFileName() {
return openFileName;
}
class EntryArrayListener extends DataModelAdaptor {
public void dataModelGrabFocus(DataModelEvent event) {
editEntryCommand();
}
public void dataModelChangeValue(DataModelEvent event) {
needSaving.setBoolean(true);
}
}
}