$darkmode
DENOPTIM
GUITextReader.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2020 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.gui;
20
21
22import java.awt.BorderLayout;
23import java.io.File;
24import java.io.PrintWriter;
25import java.io.StringWriter;
26import java.util.concurrent.atomic.AtomicInteger;
27
28import javax.swing.BorderFactory;
29import javax.swing.JButton;
30import javax.swing.JTextPane;
31import javax.swing.border.Border;
32import javax.swing.border.TitledBorder;
33
34import denoptim.exception.DENOPTIMException;
35import denoptim.io.DenoptimIO;
36
37
44public class GUITextReader extends GUICardPanel
45{
49 private static final long serialVersionUID = 1L;
50
54 public static AtomicInteger texReaderTabUID = new AtomicInteger(1);
55
56 private JTextPane txtPane;
57 private TitledBorder border;
58
59
60//-----------------------------------------------------------------------------
61
66 {
67 super(mainPanel, "Text File Content #"
68 + texReaderTabUID.getAndIncrement());
69 super.setLayout(new BorderLayout());
70 initialize();
71 }
72
73//-----------------------------------------------------------------------------
74
78 private void initialize()
79 {
80 // BorderLayout is needed to allow dynamic resizing!
81 this.setLayout(new BorderLayout());
82
83 txtPane = new JTextPane();
84 txtPane.setContentType("text");
85 txtPane.setText("No content");
86 txtPane.setEditable(false);
87 txtPane.setBackground(null);
88 Border bevel = BorderFactory.createLoweredBevelBorder();
89 border = BorderFactory.createTitledBorder(bevel, "No source of text");
90 border.setTitleJustification(TitledBorder.CENTER);
91 txtPane.setBorder(border);
92 this.add(txtPane,BorderLayout.CENTER);
93
94
95 ButtonsBar commandsPane = new ButtonsBar();
96 this.add(commandsPane, BorderLayout.SOUTH);
97 JButton btnCanc = new JButton("Close Tab");
98 btnCanc.setToolTipText("Closes this FSERun Inspector.");
99 btnCanc.addActionListener(new removeCardActionListener(this));
100 commandsPane.add(btnCanc);
101 }
102
103//-----------------------------------------------------------------------------
104
109 public void displayContent(File file)
110 {
111 String fileID = file.getAbsolutePath();
112 if (file.getAbsolutePath().length() > 80)
113 fileID = file.getName();
114
115 try
116 {
117 txtPane.setText(DenoptimIO.readText(file.getAbsolutePath()));
118 setSourceTitle(fileID);
119 } catch (DENOPTIMException e)
120 {
121 setSourceTitle("ERROR while reading '" + fileID + "'");
122 e.printStackTrace();
123 StringWriter sw = new StringWriter();
124 PrintWriter pw = new PrintWriter(sw);
125 e.printStackTrace(pw);
126 String sStackTrace = sw.toString();
127 txtPane.setText("Could not read content of file: "
128 + System.getProperty("line.separator")
129 + sStackTrace);
130 }
131 }
132
133//-----------------------------------------------------------------------------
134
135
136 private void setSourceTitle(String string)
137 {
138 border.setTitle("Content of " + string);
139 }
140
141//-----------------------------------------------------------------------------
142
143}
Standardised horizontal bar with padded components, which are meant to be JButtons.
Definition: ButtonsBar.java:36
Component add(Component comp)
Definition: ButtonsBar.java:53
Remove the card from the deck of cards and takes care of removing also the entry in the list of activ...
Class of GUI panels meant to occupy one card in the deck-of-cards layout of the main panel.
GUIMainPanel mainPanel
The main panel (cards deck)
The main panel is a deck of cards that occupies all the GUI frame.
A panel that allows to print the content of a text file into a GUI tab.
GUITextReader(GUIMainPanel mainPanel)
Constructor.
void setSourceTitle(String string)
static AtomicInteger texReaderTabUID
Unique identified for instances of this handler.
static final long serialVersionUID
Version UID.
void initialize()
Initialize the panel and add buttons.
void displayContent(File file)
Print the content of the given file in this tab's pane.
Utility methods for input/output.
static String readText(String fileName)
Read text from file.