Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
TextPrompt.java
Go to the documentation of this file.
1 package org.sleuthkit.autopsy.corecomponents;
2 
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.event.*;
8 import javax.swing.text.*;
9 
20 public final class TextPrompt extends JLabel
21  implements FocusListener, DocumentListener {
22 
23  public enum Show {
24 
28  }
29 
30  private JTextComponent component;
31  private Document document;
32 
33  private Show show;
34  private boolean showPromptOnce;
35  private int focusLost;
36 
37  public TextPrompt(String text, JTextComponent component) {
38  this(text, component, Show.ALWAYS);
39  }
40 
41  public TextPrompt(String text, JTextComponent component, Show show) {
42  this.component = component;
43  component.removeAll();
44  setShow(show);
45  document = component.getDocument();
46 
47  setText(text);
48  setFont(component.getFont());
49  setForeground(component.getForeground());
50  setBorder(new EmptyBorder(component.getInsets()));
51  setHorizontalAlignment(JLabel.LEADING);
52 
53  component.addFocusListener(this);
54  document.addDocumentListener(this);
55 
56  component.setLayout(new BorderLayout());
57  component.add(this);
59  }
60 
67  public void changeAlpha(float alpha) {
68  changeAlpha((int) (alpha * 255));
69  }
70 
77  public void changeAlpha(int alpha) {
78  alpha = alpha > 255 ? 255 : alpha < 0 ? 0 : alpha;
79 
80  Color foreground = getForeground();
81  int red = foreground.getRed();
82  int green = foreground.getGreen();
83  int blue = foreground.getBlue();
84 
85  Color withAlpha = new Color(red, green, blue, alpha);
86  super.setForeground(withAlpha);
87  }
88 
96  public void changeStyle(int style) {
97  setFont(getFont().deriveFont(style));
98  }
99 
105  public Show getShow() {
106  return show;
107  }
108 
120  public void setShow(Show show) {
121  this.show = show;
122  }
123 
129  public boolean getShowPromptOnce() {
130  return showPromptOnce;
131  }
132 
140  public void setShowPromptOnce(boolean showPromptOnce) {
141  this.showPromptOnce = showPromptOnce;
142  }
143 
148  private void checkForPrompt() {
149  // Text has been entered, remove the prompt
150 
151  if (document.getLength() > 0) {
152  setVisible(false);
153  return;
154  }
155 
156  // Prompt has already been shown once, remove it
157  if (showPromptOnce && focusLost > 0) {
158  setVisible(false);
159  return;
160  }
161 
162  // Check the Show property and component focus to determine if the
163  // prompt should be displayed.
164  if (component.hasFocus()) {
165  if (show == Show.ALWAYS
166  || show == Show.FOCUS_GAINED) {
167  setVisible(true);
168  } else {
169  setVisible(false);
170  }
171  } else {
172  if (show == Show.ALWAYS
173  || show == Show.FOCUS_LOST) {
174  setVisible(true);
175  } else {
176  setVisible(false);
177  }
178  }
179  }
180 
181 // Implement FocusListener
182  @Override
183  public void focusGained(FocusEvent e) {
184  checkForPrompt();
185  }
186 
187  @Override
188  public void focusLost(FocusEvent e) {
189  focusLost++;
190  checkForPrompt();
191  }
192 
193 // Implement DocumentListener
194  @Override
195  public void insertUpdate(DocumentEvent e) {
196  checkForPrompt();
197  }
198 
199  @Override
200  public void removeUpdate(DocumentEvent e) {
201  checkForPrompt();
202  }
203 
204  @Override
205  public void changedUpdate(DocumentEvent e) {
206  }
207 }
TextPrompt(String text, JTextComponent component, Show show)
Definition: TextPrompt.java:41
TextPrompt(String text, JTextComponent component)
Definition: TextPrompt.java:37
void setShowPromptOnce(boolean showPromptOnce)

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.