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: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 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.gui;
|
---|
38 |
|
---|
39 | import java.awt.*;
|
---|
40 | import java.awt.event.*;
|
---|
41 | import java.io.*;
|
---|
42 | import javax.swing.*;
|
---|
43 | import org.greenstone.gatherer.Configuration;
|
---|
44 | import org.greenstone.gatherer.Dictionary;
|
---|
45 | import org.greenstone.gatherer.Gatherer;
|
---|
46 | import org.greenstone.gatherer.collection.CollectionTreeNode;
|
---|
47 | import org.greenstone.gatherer.file.FileManager;
|
---|
48 |
|
---|
49 | public class NewFolderOrFilePrompt
|
---|
50 | extends JDialog
|
---|
51 | implements ActionListener {
|
---|
52 | private CollectionTreeNode node;
|
---|
53 | private JButton cancel_button;
|
---|
54 | private JButton ok_button;
|
---|
55 | private JTextField name_field;
|
---|
56 | private String name;
|
---|
57 |
|
---|
58 | private int type;
|
---|
59 | private String extension="";
|
---|
60 | static final private Dimension SIZE = new Dimension(350,115);
|
---|
61 |
|
---|
62 | public NewFolderOrFilePrompt(CollectionTreeNode node, int type, String extension) {
|
---|
63 | super(Gatherer.g_man, true);
|
---|
64 | this.setComponentOrientation(Dictionary.getOrientation());
|
---|
65 | if (type == FileManager.FILE_TYPE) {
|
---|
66 | setTitle(Dictionary.get("NewFolderOrFilePrompt.Title_File"));
|
---|
67 | } else {
|
---|
68 | setTitle(Dictionary.get("NewFolderOrFilePrompt.Title_Folder"));
|
---|
69 | }
|
---|
70 | this.type = type;
|
---|
71 | if (extension != null) {
|
---|
72 | this.extension = extension;
|
---|
73 | }
|
---|
74 | this.node = node;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void actionPerformed(ActionEvent event) {
|
---|
78 | if (event.getSource() == ok_button) {
|
---|
79 | name = name_field.getText()+extension;
|
---|
80 | }
|
---|
81 | else if(event.getSource() == cancel_button) {
|
---|
82 | name = null;
|
---|
83 | }
|
---|
84 | dispose();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public String display() {
|
---|
88 | setSize(SIZE);
|
---|
89 | JPanel content_pane = (JPanel) getContentPane();
|
---|
90 | content_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
91 |
|
---|
92 | JPanel labels_pane = new JPanel();
|
---|
93 | labels_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
94 | JPanel fields_pane = new JPanel();
|
---|
95 | fields_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
96 | JPanel info_pane = new JPanel();
|
---|
97 | info_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
98 |
|
---|
99 | JLabel destination_label = new JLabel(Dictionary.get("NewFolderOrFilePrompt.Destination_Name"));
|
---|
100 | destination_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
101 |
|
---|
102 | JTextField destination_textfield = new JTextField(node.getFile().getName());
|
---|
103 | destination_textfield.setComponentOrientation(Dictionary.getOrientation());
|
---|
104 | destination_textfield.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
|
---|
105 | destination_textfield.setEditable(false);
|
---|
106 |
|
---|
107 | JLabel name_label = new JLabel();
|
---|
108 | name_label.setComponentOrientation(Dictionary.getOrientation());
|
---|
109 | name_field = new JTextField(getAutomaticName());
|
---|
110 | name_field.setComponentOrientation(Dictionary.getOrientation());
|
---|
111 | name_field.selectAll(); // select all the text in the textfield
|
---|
112 |
|
---|
113 | if (type == FileManager.FILE_TYPE) {
|
---|
114 | name_label.setText(Dictionary.get("NewFolderOrFilePrompt.File_Name"));
|
---|
115 | name_field.setToolTipText(Dictionary.get("NewFolderOrFilePrompt.File_Name_Tooltip"));
|
---|
116 | } else {
|
---|
117 | name_label.setText(Dictionary.get("NewFolderOrFilePrompt.Folder_Name"));
|
---|
118 | name_field.setToolTipText(Dictionary.get("NewFolderOrFilePrompt.Folder_Name_Tooltip"));
|
---|
119 | }
|
---|
120 |
|
---|
121 | JPanel button_pane = new JPanel();
|
---|
122 | button_pane.setComponentOrientation(Dictionary.getOrientation());
|
---|
123 | ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
|
---|
124 |
|
---|
125 | cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
|
---|
126 |
|
---|
127 | // Connection
|
---|
128 | cancel_button.addActionListener(this);
|
---|
129 | ok_button.addActionListener(this);
|
---|
130 | getRootPane().setDefaultButton(ok_button);
|
---|
131 |
|
---|
132 | // Layout
|
---|
133 | labels_pane.setLayout(new GridLayout(2,1, 5,0));
|
---|
134 | labels_pane.add(destination_label);
|
---|
135 | labels_pane.add(name_label);
|
---|
136 |
|
---|
137 | fields_pane.setLayout(new GridLayout(2,1,0,5));
|
---|
138 | fields_pane.add(destination_textfield);
|
---|
139 | fields_pane.add(name_field);
|
---|
140 |
|
---|
141 | info_pane.setLayout(new BorderLayout(5,0));
|
---|
142 | info_pane.add(labels_pane, BorderLayout.LINE_START);
|
---|
143 | info_pane.add(fields_pane, BorderLayout.CENTER);
|
---|
144 |
|
---|
145 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
146 | button_pane.setLayout(new GridLayout(1,2,0,5));
|
---|
147 | button_pane.add(ok_button);
|
---|
148 | button_pane.add(cancel_button);
|
---|
149 |
|
---|
150 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
151 | content_pane.setLayout(new BorderLayout());
|
---|
152 | content_pane.add(info_pane, BorderLayout.CENTER);
|
---|
153 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
154 |
|
---|
155 | // Display
|
---|
156 | Rectangle frame_bounds = Gatherer.g_man.getBounds();
|
---|
157 | setLocation(frame_bounds.x + (frame_bounds.width - SIZE.width) / 2, frame_bounds.y + (frame_bounds.height - SIZE.height) / 2);
|
---|
158 | setVisible(true);
|
---|
159 | return name;
|
---|
160 | }
|
---|
161 |
|
---|
162 | private String getAutomaticName()
|
---|
163 | {
|
---|
164 | File file = node.getFile();
|
---|
165 | String default_name;
|
---|
166 | if (type == FileManager.FILE_TYPE) {
|
---|
167 | default_name = Dictionary.get("NewFolderOrFilePrompt.Default_File_Name");
|
---|
168 | } else {
|
---|
169 | default_name = Dictionary.get("NewFolderOrFilePrompt.Default_Folder_Name");
|
---|
170 | }
|
---|
171 |
|
---|
172 | File temp_file = new File(file, default_name+extension);
|
---|
173 | int count = 1;
|
---|
174 | while (temp_file.exists()) {
|
---|
175 | temp_file = new File(file, default_name + " " + count+extension);
|
---|
176 | count++;
|
---|
177 | }
|
---|
178 | if (extension.equals("")) {
|
---|
179 | return temp_file.getName();
|
---|
180 | }
|
---|
181 | String name = temp_file.getName();
|
---|
182 | // remove the extension
|
---|
183 | return name.substring(0, name.length()-extension.length());
|
---|
184 |
|
---|
185 | }
|
---|
186 | }
|
---|