Autopsy  4.21.0
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, null);
39  }
40 
41  public TextPrompt(String text, JTextComponent component, Show show) {
42  this(text, component, show, null);
43  }
44 
45  public TextPrompt(String text, JTextComponent component, String layoutConstraint) {
46  this(text, component, Show.ALWAYS, layoutConstraint);
47  }
48 
49  public TextPrompt(String text, JTextComponent component, Show show, String layoutConstraint) {
50  this.component = component;
51  component.removeAll();
52  setShow(show);
53  document = component.getDocument();
54 
55  setText(text);
56  setFont(component.getFont());
57  setForeground(component.getForeground());
58  setBorder(new EmptyBorder(component.getInsets()));
59  setHorizontalAlignment(JLabel.LEADING);
60 
61  component.addFocusListener(this);
62  document.addDocumentListener(this);
63 
64  component.setLayout(new BorderLayout());
65  if (layoutConstraint == null) {
66  component.add(this);
67  } else {
68  component.add(this, layoutConstraint);
69  }
71  }
72 
79  public void changeAlpha(float alpha) {
80  changeAlpha((int) (alpha * 255));
81  }
82 
89  public void changeAlpha(int alpha) {
90  alpha = alpha > 255 ? 255 : alpha < 0 ? 0 : alpha;
91 
92  Color foreground = getForeground();
93  int red = foreground.getRed();
94  int green = foreground.getGreen();
95  int blue = foreground.getBlue();
96 
97  Color withAlpha = new Color(red, green, blue, alpha);
98  super.setForeground(withAlpha);
99  }
100 
108  public void changeStyle(int style) {
109  setFont(getFont().deriveFont(style));
110  }
111 
117  public Show getShow() {
118  return show;
119  }
120 
132  public void setShow(Show show) {
133  this.show = show;
134  }
135 
141  public boolean getShowPromptOnce() {
142  return showPromptOnce;
143  }
144 
152  public void setShowPromptOnce(boolean showPromptOnce) {
153  this.showPromptOnce = showPromptOnce;
154  }
155 
160  private void checkForPrompt() {
161  // Text has been entered, remove the prompt
162 
163  if (document.getLength() > 0) {
164  setVisible(false);
165  return;
166  }
167 
168  // Prompt has already been shown once, remove it
169  if (showPromptOnce && focusLost > 0) {
170  setVisible(false);
171  return;
172  }
173 
174  // Check the Show property and component focus to determine if the
175  // prompt should be displayed.
176  if (component.hasFocus()) {
177  if (show == Show.ALWAYS
178  || show == Show.FOCUS_GAINED) {
179  setVisible(true);
180  } else {
181  setVisible(false);
182  }
183  } else {
184  if (show == Show.ALWAYS
185  || show == Show.FOCUS_LOST) {
186  setVisible(true);
187  } else {
188  setVisible(false);
189  }
190  }
191  }
192 
193 // Implement FocusListener
194  @Override
195  public void focusGained(FocusEvent e) {
196  checkForPrompt();
197  }
198 
199  @Override
200  public void focusLost(FocusEvent e) {
201  focusLost++;
202  checkForPrompt();
203  }
204 
205 // Implement DocumentListener
206  @Override
207  public void insertUpdate(DocumentEvent e) {
208  checkForPrompt();
209  }
210 
211  @Override
212  public void removeUpdate(DocumentEvent e) {
213  checkForPrompt();
214  }
215 
216  @Override
217  public void changedUpdate(DocumentEvent e) {
218  }
219 }
TextPrompt(String text, JTextComponent component, Show show)
Definition: TextPrompt.java:41
TextPrompt(String text, JTextComponent component, String layoutConstraint)
Definition: TextPrompt.java:45
TextPrompt(String text, JTextComponent component)
Definition: TextPrompt.java:37
void setShowPromptOnce(boolean showPromptOnce)
TextPrompt(String text, JTextComponent component, Show show, String layoutConstraint)
Definition: TextPrompt.java:49

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.