Skip to content

Commit

Permalink
Static TextEditorContext to hold the current home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
l-gu committed Apr 25, 2019
1 parent bd27458 commit f45d802
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 56 deletions.
60 changes: 60 additions & 0 deletions src/main/java/org/telosys/tools/editor/TextEditorContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (C) 2015-2019 Telosys project org. ( http://www.telosys.org/ )
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.telosys.tools.editor;

import java.io.File;

public class TextEditorContext {

private static String homeDirectory = null ;

/**
* Private constructor
*/
private TextEditorContext() {

}

/**
* Set the current HOME directory
* @param directory
*/
protected static void setHomeDirectory(String directory) {
homeDirectory = directory ;
}

/**
* Returns the current HOME directory as String (or null if not defined)
* @return
*/
public static String getHomeDirectory() {
return homeDirectory ;
}

/**
* Returns the current HOME directory as File (or null if not defined)
* @return
*/
public static File getHomeDirectoryFile() {
if ( homeDirectory != null ) {
return new File(homeDirectory);
}
else {
return null ;
}
}

}
62 changes: 21 additions & 41 deletions src/main/java/org/telosys/tools/editor/TextEditorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,56 @@

import org.telosys.tools.editor.components.TextEditor;

/**
* This class is designed to be used as the interface to manage the TextEditor from the CLI
*
* @author Laurent GUERIN
*
*/
public class TextEditorManager {

private static String homeDirectory = null ;

private static TextEditor textEditor = null;

/**
* Private constructor
*/
private TextEditorManager() {

}

public static void setHomeDir(String directory) {
homeDirectory = directory ;
if ( textEditor != null && homeDirectory != null ) {
// Update the current directory in the Editor
textEditor.setCurrentDir(new File(homeDirectory));
}
/**
* Set the current HOME directory
* @param directory
*/
public static void setHomeDirectory(String directory) {
TextEditorContext.setHomeDirectory(directory);
}

/**
* Just open the editor (without file to edit)
* Just init (if necessary) and open the editor (without file to edit)
*/
public static void openEditor() {
initTextEditor(null) ;
initTextEditor() ;
textEditor.putOnFront();
}

/**
* Init (if necessary) and open the editor to edit the given file
* @param file
*/
public static void editFile(File file) {
// // If no current TextEditor create it
// if ( textEditor == null ) {
// File homeDir = getHomeDir(file);
// textEditor = new TextEditor(homeDir);
// }
initTextEditor(file) ;
initTextEditor() ;
// Use current TextEditor to edit the given file
textEditor.editFile(file);
textEditor.putOnFront();
}

/**
* Init the TextEditor (if no current TextEditor create it)
* @param file
*/
private static void initTextEditor(File file) {
private static void initTextEditor() {
if ( textEditor == null ) { // not yet created
// try to determine the home directory
File homeDir = null;
if ( homeDirectory != null ) {
homeDir = new File(homeDirectory);
}
else {
if ( file != null ) {
homeDir = getHomeDir(file);
}
}
// Create the TextEditor instance
textEditor = new TextEditor(homeDir);
}
}

private static File getHomeDir(File file) {
if ( homeDirectory != null ) {
// Home directory has been set before
return new File(homeDirectory) ;
}
else {
// Not supposed to happen
return file.getParentFile();
textEditor = new TextEditor();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import javax.swing.JOptionPane;

import org.telosys.tools.editor.TextEditorContext;

/**
* About dialog box
*
Expand All @@ -28,6 +30,9 @@ public static void show() {
String title = "Debug";

String msg = "Debug information :"
+ "\n"
+ "Home directory : " + TextEditorContext.getHomeDirectory()
+ "\n"
+ "\n"
+ "tabbedPane.getTabCount() : " + DebugVariables.tabbedPane.getTabCount()
+ "\n"
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/org/telosys/tools/editor/components/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.telosys.tools.editor.TextEditorContext;
import org.telosys.tools.editor.components.tabs.ButtonTabComponent;
import org.telosys.tools.editor.components.textarea.TxDocumentListener;
import org.telosys.tools.editor.components.textarea.TxScrollPane;
Expand All @@ -58,13 +59,10 @@ public class TextEditor extends JFrame {
private final JTabbedPane tabbedPane;
private final JLabel bottomLabel;

private File currentDir;

/**
* Constructor
* @param initialDirectory the initial directory
*/
public TextEditor(File initialDirectory) {
public TextEditor() {
super();

// Set Operating System Look & Feel
Expand All @@ -77,8 +75,6 @@ public TextEditor(File initialDirectory) {

frame = this;

currentDir = initialDirectory;

setSize(600, 600);
setLocationRelativeTo(null);

Expand Down Expand Up @@ -133,14 +129,6 @@ public void stateChanged(ChangeEvent e) {
DebugVariables.tabbedPane = this.tabbedPane;
}

/**
* Set the current directory (used by the 'FileChooser' for 'Open/Save as'
* @param currentDir
*/
public void setCurrentDir(File currentDir) {
this.currentDir = currentDir ;
}

@Override
public void setVisible(final boolean visible) {
// // make sure that frame is marked as not disposed if it is asked to be
Expand Down Expand Up @@ -265,7 +253,7 @@ private void setIconImage(String imagePath) {
}

protected void actionOpen() {
JFileChooser fileChooser = createFileChooser("Open file", "Open", currentDir);
JFileChooser fileChooser = createFileChooser("Open file", "Open", TextEditorContext.getHomeDirectoryFile());
int returnValue = fileChooser.showOpenDialog(this);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
Expand Down

0 comments on commit f45d802

Please sign in to comment.