1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.app; 33 34 import com.oracle.javafx.scenebuilder.app.i18n.I18N; 35 import com.oracle.javafx.scenebuilder.kit.editor.EditorController; 36 import com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform; 37 import com.oracle.javafx.scenebuilder.kit.editor.panel.util.dialog.ErrorDialog; 38 39 import java.io.File; 40 import java.io.FileInputStream; 41 import java.io.IOException; 42 import java.io.InputStreamReader; 43 import java.nio.charset.Charset; 44 import java.util.PropertyResourceBundle; 45 import java.util.ResourceBundle; 46 47 import javafx.stage.FileChooser; 48 49 /** 50 * 51 */ 52 class ResourceController { 53 54 private final DocumentWindowController documentWindowController; 55 private File resourceFile; 56 57 public ResourceController(DocumentWindowController dwc) { 58 this.documentWindowController = dwc; 59 } 60 61 public File getResourceFile() { 62 return resourceFile; 63 } 64 65 public void setResourceFile(File file) { 66 if (file != null) { 67 if (readPropertyResourceBundle(file) == null) { 68 // Property file syntax is probably incorrect 69 70 } else { 71 resourceFile = file; 72 resourceFileDidChange(); 73 } 74 } 75 } 76 77 public void performSetResource() { 78 // Open a file chooser for *.properties & *.bss 79 FileChooser fileChooser = new FileChooser(); 80 fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(I18N.getString("resource.filechooser.filter.msg"), 81 "*.properties")); //NOI18N 82 fileChooser.setInitialDirectory(EditorController.getNextInitialDirectory()); 83 File newResourceFile = fileChooser.showOpenDialog(documentWindowController.getStage()); 84 // Keep track of the user choice for next time 85 if (newResourceFile != null) { 86 EditorController.updateNextInitialDirectory(newResourceFile); 87 } 88 89 setResourceFile(newResourceFile); 90 } 91 92 public void performRemoveResource() { 93 assert resourceFile != null; 94 resourceFile = null; 95 resourceFileDidChange(); 96 } 97 98 public void performRevealResource() { 99 assert resourceFile != null; 100 try { 101 EditorPlatform.revealInFileBrowser(resourceFile); 102 } catch (IOException ioe) { 103 final ErrorDialog errorDialog = new ErrorDialog(null); 104 errorDialog.setTitle(I18N.getString("error.file.reveal.title")); 105 errorDialog.setMessage(I18N.getString("error.file.reveal.message")); 106 errorDialog.setDetails(I18N.getString("error.filesystem.details")); 107 errorDialog.setDebugInfoWithThrowable(ioe); 108 errorDialog.showAndWait(); 109 } 110 } 111 112 public void performReloadResource() { 113 assert resourceFile != null; 114 resourceFileDidChange(); 115 } 116 117 /* 118 * Private 119 */ 120 121 private void resourceFileDidChange() { 122 ResourceBundle resources; 123 124 if (resourceFile != null) { 125 resources = readPropertyResourceBundle(resourceFile); 126 assert resources != null; 127 } else { 128 resources = null; 129 } 130 131 documentWindowController.getEditorController().setResources(resources); 132 documentWindowController.getWatchingController().update(); 133 } 134 135 136 private static PropertyResourceBundle readPropertyResourceBundle(File f) { 137 PropertyResourceBundle result; 138 try { 139 result = new PropertyResourceBundle(new InputStreamReader(new FileInputStream(f), Charset.forName("UTF-8"))); //NOI18N 140 } catch (IOException ex) { 141 result = null; 142 } 143 return result; 144 } 145 }