001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.gui.se.pane;
017
018 import java.awt.BorderLayout;
019 import java.awt.Container;
020 import java.awt.Dimension;
021 import java.awt.Font;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.ActionListener;
024
025 import javax.swing.BoxLayout;
026 import javax.swing.ImageIcon;
027 import javax.swing.JButton;
028 import javax.swing.JFrame;
029 import javax.swing.JPanel;
030 import javax.swing.JScrollPane;
031 import javax.swing.JViewport;
032 import javax.swing.text.BadLocationException;
033 import javax.swing.text.DefaultStyledDocument;
034 import javax.swing.text.MutableAttributeSet;
035 import javax.swing.text.SimpleAttributeSet;
036 import javax.swing.text.StyleConstants;
037 import javax.swing.text.StyleContext;
038 import javax.swing.text.StyledDocument;
039
040 import org.qedeq.base.trace.Trace;
041 import org.qedeq.gui.se.element.CPTextPane;
042 import org.qedeq.gui.se.util.GuiHelper;
043
044 import com.jgoodies.forms.builder.ButtonBarBuilder;
045
046 /**
047 * Global log. All events are displayed here.
048 *
049 * @author Michael Meyling
050 */
051
052 public class TextPaneWindow extends JFrame {
053
054 /** This class. */
055 private static final Class CLASS = TextPaneWindow.class;
056
057 /** Common text attributes. */
058 private final SimpleAttributeSet attrs = new SimpleAttributeSet();
059
060 /** Here is the text. */
061 private CPTextPane textPane;
062
063 /** Word wrap on?. */
064 private boolean wordWrap;
065
066
067 /**
068 * Creates new panel.
069 *
070 * @param title Window title.
071 * @param icon Window icon.
072 * @param text Display this text.
073 */
074 public TextPaneWindow(final String title, final ImageIcon icon, final String text) {
075 super(title);
076 if (icon != null) {
077 setIconImage(icon.getImage());
078 }
079 final String method = "Constructor";
080 Trace.begin(CLASS, this, method);
081 try {
082 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
083 setupView(text);
084 } catch (Throwable e) {
085 Trace.fatal(CLASS, this, "Initalization of TextPaneWindow failed.", method, e);
086 } finally {
087 Trace.end(CLASS, this, method);
088 }
089 }
090
091 /**
092 * Assembles the GUI components of the panel.
093 *
094 * @param text Display this text.
095 */
096 public final void setupView(final String text) {
097 final Container content = getContentPane();
098 content.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
099 JPanel allOptions = new JPanel();
100 allOptions.setBorder(GuiHelper.getEmptyBorder());
101 allOptions.setLayout(new BoxLayout(allOptions, BoxLayout.Y_AXIS));
102 allOptions.add(createTextPanel(text));
103 content.add(allOptions);
104
105 ButtonBarBuilder bbuilder = ButtonBarBuilder.createLeftToRightBuilder();
106
107 JButton ok = new JButton("OK");
108 ok.addActionListener(new ActionListener() {
109 public void actionPerformed(final ActionEvent actionEvent) {
110 TextPaneWindow.this.dispose();
111 }
112 });
113
114 JButton increaseFontSize = new JButton("+1");
115 increaseFontSize.addActionListener(new ActionListener() {
116 public void actionPerformed(final ActionEvent actionEvent) {
117 final MutableAttributeSet attr = TextPaneWindow.this.textPane.getInputAttributes();
118 final int size = StyleConstants.getFontSize(attr);
119 StyleConstants.setFontSize(attr, size + 1);
120 final StyledDocument doc = TextPaneWindow.this.textPane.getStyledDocument();
121 doc.setCharacterAttributes(0, doc.getLength(), attr, true);
122 }
123 });
124
125 JButton decreaseFontSize = new JButton("-1");
126 decreaseFontSize.addActionListener(new ActionListener() {
127 public void actionPerformed(final ActionEvent actionEvent) {
128 final MutableAttributeSet attr = TextPaneWindow.this.textPane.getInputAttributes();
129 final int size = StyleConstants.getFontSize(attr);
130 if (size > 1) {
131 StyleConstants.setFontSize(attr, size - 1);
132 final StyledDocument doc = TextPaneWindow.this.textPane.getStyledDocument();
133 doc.setCharacterAttributes(0, doc.getLength(), attr, true);
134 }
135 }
136 });
137
138 final JButton wrap = new JButton("wrap on");
139 wrap.addActionListener(new ActionListener() {
140 public void actionPerformed(final ActionEvent actionEvent) {
141 TextPaneWindow.this.wordWrap = !TextPaneWindow.this.wordWrap;
142 if (TextPaneWindow.this.wordWrap) {
143 wrap.setText("wrap on");
144 } else {
145 wrap.setText("wrap off");
146 }
147 Dimension d = TextPaneWindow.this.textPane.getSize();
148 // TODO 20101116 m31: Quick and Dirty hack to force a new layout and refresh
149 d.setSize(d.height, d.width - 1);
150 TextPaneWindow.this.textPane.setSize(d);
151 d.setSize(d.height, d.width + 1);
152 TextPaneWindow.this.textPane.setSize(d);
153 }
154 });
155
156 bbuilder.addGriddedButtons(new JButton[] {wrap, decreaseFontSize, increaseFontSize, ok});
157
158 final JPanel buttons = bbuilder.getPanel();
159 content.add(GuiHelper.addSpaceAndAlignRight(buttons));
160
161 // let the container calculate the ideal size
162 pack();
163
164 final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
165 int height = getHeight();
166 int width = getWidth();
167 if (screenSize.height - getHeight() < 50) {
168 height = screenSize.height - 50;
169 }
170 if (screenSize.width - width < 40) {
171 width = screenSize.width - 40;
172 } else if (screenSize.width - width > 40) {
173 width = width + 20;
174 }
175 setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
176
177 // final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
178 // setBounds((screenSize.width - getWidth()) / 3, (screenSize.height - getHeight()) / 3,
179 // 800, 600);
180 }
181
182 /**
183 * Creates panel with text pane.
184 *
185 * @param text Display this text.
186 * @return Created panel.
187 */
188 private final JPanel createTextPanel(final String text) {
189 textPane = new CPTextPane(false) {
190 public boolean getScrollableTracksViewportWidth() {
191 return TextPaneWindow.this.wordWrap;
192 }
193 };
194 // TODO 20110204 m31: make configurable in GUI
195 // GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
196 // Font[] fonts = e.getAllFonts(); // Get the fonts
197 // for (Font f : fonts) {
198 // System.out.println(f.getFontName());
199 // }
200 // textPane.setFont(new Font("Lucida Sans Unicode", Font.PLAIN, textPane.getFont().getSize()));
201 // textPane.setFont(new Font("Lucida Console", Font.PLAIN, textPane.getFont().getSize()));
202 // textPane.setFont(new Font("Lucida Sans Typewriter", Font.PLAIN, textPane.getFont().getSize()));
203 textPane.setFont(new Font("Monospaced", Font.PLAIN, textPane.getFont().getSize()));
204 final StyleContext sc = new StyleContext();
205 final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
206 try {
207 doc.insertString(0, text, attrs);
208 } catch (BadLocationException e) {
209 // should not occur
210 throw new RuntimeException(e);
211 }
212 textPane.setDocument(doc);
213 final JPanel panel = new JPanel();
214 textPane.setDragEnabled(true);
215 textPane.setAutoscrolls(true);
216 textPane.setCaretPosition(0);
217 textPane.setEditable(false);
218 textPane.getCaret().setVisible(false);
219 // final DefaultCaret caret = (DefaultCaret) text.getCaret();
220 // caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // LATER mime JRE 1.5
221 textPane.setFocusable(true);
222 final JScrollPane scroller = new JScrollPane();
223 final JViewport vp = scroller.getViewport();
224 vp.add(textPane);
225 vp.setBackground(textPane.getBackground());
226 panel.setLayout(new BorderLayout(1, 1));
227 panel.add(scroller);
228 return panel;
229 }
230
231 }
|