1 /*
2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.awt.X11;
27
28 import java.awt.*;
29 import javax.swing.*;
30 import java.awt.event.*;
31 import java.awt.peer.*;
32 import java.io.*;
33 import java.util.Locale;
34 import java.util.Arrays;
35 import com.sun.java.swing.plaf.motif.*;
36 import javax.swing.plaf.ComponentUI;
37 import java.security.AccessController;
38 import java.security.PrivilegedAction;
39 import sun.util.logging.PlatformLogger;
40 import sun.awt.AWTAccessor;
41
42 class XFileDialogPeer extends XDialogPeer implements FileDialogPeer, ActionListener, ItemListener, KeyEventDispatcher, XChoicePeerListener {
43 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XFileDialogPeer");
44
45 FileDialog target;
46
47 // This variable holds value exactly the same as value of the 'target.file' variable except:
48 // 1) is changed to null after quit (see handleQuitButton())
49 // 2) keep the same value if 'target.file' is incorrect (see setFile())
50 // It's not clear HOW we used it
51 // We should think about existence of this variable
52 String file;
53
54 String dir;
55
56 String title;
57 int mode;
58 FilenameFilter filter;
59
60 private static final int PATH_CHOICE_WIDTH = 20;
61
62 // Seems that the purpose of this variable is cashing of 'target.file' variable in order to help method show()
63 // We should think about using 'target.file' instead of 'savedFile'
269
270 // add buttons to GridBagLayout Buttons
271 addComponent(openButton, gblButtons, gbc, 0, 0, 1,
272 GridBagConstraints.WEST, (Container)buttons,
273 1, 0, GridBagConstraints.NONE, noInset);
274 addComponent(filterButton, gblButtons, gbc, 1, 0, 1,
275 GridBagConstraints.CENTER, (Container)buttons,
276 1, 0, GridBagConstraints.NONE, noInset);
277 addComponent(cancelButton, gblButtons, gbc, 2, 0, 1,
278 GridBagConstraints.EAST, (Container)buttons,
279 1, 0, GridBagConstraints.NONE, noInset);
280
281 // add ButtonPanel to the GridBagLayout of this class
282 addComponent(buttons, gbl, gbc, 0, 9, 2,
283 GridBagConstraints.WEST, (Container)fileDialog,
284 1, 0, GridBagConstraints.HORIZONTAL, buttonsInset);
285
286 fileDialog.setSize(400, 400);
287
288 // Update choice's popup width
289 XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
290 choicePeer.setDrawSelectedItem(false);
291 choicePeer.setAlignUnder(pathField);
292
293 filterField.addActionListener(this);
294 selectionField.addActionListener(this);
295 directoryList.addActionListener(this);
296 directoryList.addItemListener(this);
297 fileList.addItemListener(this);
298 fileList.addActionListener(this);
299 openButton.addActionListener(this);
300 filterButton.addActionListener(this);
301 cancelButton.addActionListener(this);
302 pathChoice.addItemListener(this);
303 pathField.addActionListener(this);
304
305 // b6227750 FileDialog is not disposed when clicking the 'close' (X) button on the top right corner, XToolkit
306 target.addWindowListener(
307 new WindowAdapter(){
308 public void windowClosing(WindowEvent e){
309 handleCancel();
625 handleSelection(selectionField.getText());
626 handleQuitButton();
627 } else if (filterField == ((TextField)source)) {
628 handleFilter(filterField.getText());
629 } else if (pathField == ((TextField)source)) {
630 target.setDirectory(pathField.getText());
631 }
632 } else if (source instanceof List) {
633 if (directoryList == ((List)source)) {
634 //handleFilter( actionCommand + getFileName( filterField.getText() ) );
635 if (updateDirectoryByUserAction(actionCommand)){
636 handleFilter( getFileName( filterField.getText() ) );
637 }
638 } else if (fileList == ((List)source)) {
639 handleSelection( actionCommand );
640 handleQuitButton();
641 }
642 }
643 }
644
645 @SuppressWarnings("deprecation")
646 public boolean dispatchKeyEvent(KeyEvent keyEvent) {
647 int id = keyEvent.getID();
648 int keyCode = keyEvent.getKeyCode();
649
650 if (id == KeyEvent.KEY_PRESSED && keyCode == KeyEvent.VK_ESCAPE) {
651 synchronized (target.getTreeLock()) {
652 Component comp = (Component) keyEvent.getSource();
653 while (comp != null) {
654 // Fix for 6240084 Disposing a file dialog when the drop-down is active does not dispose the dropdown menu, on Xtoolkit
655 // See also 6259493
656 if (comp == pathChoice) {
657 XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
658 if (choicePeer.isUnfurled()){
659 return false;
660 }
661 }
662 if (comp.getPeer() == this) {
663 handleCancel();
664 return true;
665 }
666 comp = comp.getParent();
667 }
668 }
669 }
670
671 return false;
672 }
673
674
675 /**
676 * set the file
677 */
678 public void setFile(String file) {
679
680 if (file == null) {
681 this.file = null;
682 return;
779 }
780
781 // 03/02/2005 b5097243 Pressing 'ESC' on a file dlg does not dispose the dlg on Xtoolkit
782 @SuppressWarnings("deprecation")
783 public void setVisible(boolean b){
784 if (fileDialog == null) {
785 init(target);
786 }
787
788 if (savedDir != null || userDir != null) {
789 setDirectory(savedDir != null ? savedDir : userDir);
790 }
791
792 if (savedFile != null) {
793 // Actually in Motif implementation lost file value which was saved after prevously showing
794 // Seems we shouldn't restore Motif behaviour in this case
795 setFile(savedFile);
796 }
797
798 super.setVisible(b);
799 if (b == true){
800 // See 6240074 for more information
801 XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
802 choicePeer.addXChoicePeerListener(this);
803 KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
804 }else{
805 // See 6240074 for more information
806 XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
807 choicePeer.removeXChoicePeerListener();
808 KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
809 }
810
811 selectionField.requestFocusInWindow();
812 }
813
814 /*
815 * Adding items to the path choice based on the text string
816 * See 6240074 for more information
817 */
818 public void addItemsToPathChoice(String text){
819 String dirList[] = getDirList(text);
820 for (int i = 0; i < dirList.length; i++) pathChoice.addItem(dirList[i]);
821 }
822
823 /*
824 * Refresh the unfurled choice at the time of the opening choice according to the text of the path field
825 * See 6240074 for more information
826 */
827 public void unfurledChoiceOpening(ListHelper choiceHelper){
828
|
1 /*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.awt.X11;
27
28 import java.awt.*;
29 import javax.swing.*;
30 import java.awt.event.*;
31 import java.awt.peer.*;
32 import java.io.*;
33 import java.util.Locale;
34 import java.util.Arrays;
35 import java.security.AccessController;
36 import java.security.PrivilegedAction;
37
38 import sun.awt.AWTAccessor.ComponentAccessor;
39 import sun.util.logging.PlatformLogger;
40 import sun.awt.AWTAccessor;
41
42 class XFileDialogPeer extends XDialogPeer
43 implements FileDialogPeer, ActionListener, ItemListener,
44 KeyEventDispatcher, XChoicePeerListener {
45
46 private static final PlatformLogger log =
47 PlatformLogger.getLogger("sun.awt.X11.XFileDialogPeer");
48
49 FileDialog target;
50
51 // This variable holds value exactly the same as value of the 'target.file' variable except:
52 // 1) is changed to null after quit (see handleQuitButton())
53 // 2) keep the same value if 'target.file' is incorrect (see setFile())
54 // It's not clear HOW we used it
55 // We should think about existence of this variable
56 String file;
57
58 String dir;
59
60 String title;
61 int mode;
62 FilenameFilter filter;
63
64 private static final int PATH_CHOICE_WIDTH = 20;
65
66 // Seems that the purpose of this variable is cashing of 'target.file' variable in order to help method show()
67 // We should think about using 'target.file' instead of 'savedFile'
273
274 // add buttons to GridBagLayout Buttons
275 addComponent(openButton, gblButtons, gbc, 0, 0, 1,
276 GridBagConstraints.WEST, (Container)buttons,
277 1, 0, GridBagConstraints.NONE, noInset);
278 addComponent(filterButton, gblButtons, gbc, 1, 0, 1,
279 GridBagConstraints.CENTER, (Container)buttons,
280 1, 0, GridBagConstraints.NONE, noInset);
281 addComponent(cancelButton, gblButtons, gbc, 2, 0, 1,
282 GridBagConstraints.EAST, (Container)buttons,
283 1, 0, GridBagConstraints.NONE, noInset);
284
285 // add ButtonPanel to the GridBagLayout of this class
286 addComponent(buttons, gbl, gbc, 0, 9, 2,
287 GridBagConstraints.WEST, (Container)fileDialog,
288 1, 0, GridBagConstraints.HORIZONTAL, buttonsInset);
289
290 fileDialog.setSize(400, 400);
291
292 // Update choice's popup width
293 XChoicePeer choicePeer = AWTAccessor.getComponentAccessor()
294 .getPeer(pathChoice);
295 choicePeer.setDrawSelectedItem(false);
296 choicePeer.setAlignUnder(pathField);
297
298 filterField.addActionListener(this);
299 selectionField.addActionListener(this);
300 directoryList.addActionListener(this);
301 directoryList.addItemListener(this);
302 fileList.addItemListener(this);
303 fileList.addActionListener(this);
304 openButton.addActionListener(this);
305 filterButton.addActionListener(this);
306 cancelButton.addActionListener(this);
307 pathChoice.addItemListener(this);
308 pathField.addActionListener(this);
309
310 // b6227750 FileDialog is not disposed when clicking the 'close' (X) button on the top right corner, XToolkit
311 target.addWindowListener(
312 new WindowAdapter(){
313 public void windowClosing(WindowEvent e){
314 handleCancel();
630 handleSelection(selectionField.getText());
631 handleQuitButton();
632 } else if (filterField == ((TextField)source)) {
633 handleFilter(filterField.getText());
634 } else if (pathField == ((TextField)source)) {
635 target.setDirectory(pathField.getText());
636 }
637 } else if (source instanceof List) {
638 if (directoryList == ((List)source)) {
639 //handleFilter( actionCommand + getFileName( filterField.getText() ) );
640 if (updateDirectoryByUserAction(actionCommand)){
641 handleFilter( getFileName( filterField.getText() ) );
642 }
643 } else if (fileList == ((List)source)) {
644 handleSelection( actionCommand );
645 handleQuitButton();
646 }
647 }
648 }
649
650 public boolean dispatchKeyEvent(KeyEvent keyEvent) {
651 int id = keyEvent.getID();
652 int keyCode = keyEvent.getKeyCode();
653
654 if (id == KeyEvent.KEY_PRESSED && keyCode == KeyEvent.VK_ESCAPE) {
655 synchronized (target.getTreeLock()) {
656 Component comp = (Component) keyEvent.getSource();
657 while (comp != null) {
658 // Fix for 6240084 Disposing a file dialog when the drop-down is active does not dispose the dropdown menu, on Xtoolkit
659 // See also 6259493
660 ComponentAccessor acc = AWTAccessor.getComponentAccessor();
661 if (comp == pathChoice) {
662 XChoicePeer choicePeer = acc.getPeer(pathChoice);
663 if (choicePeer.isUnfurled()){
664 return false;
665 }
666 }
667 Object peer = acc.getPeer(comp);
668 if (peer == this) {
669 handleCancel();
670 return true;
671 }
672 comp = comp.getParent();
673 }
674 }
675 }
676
677 return false;
678 }
679
680
681 /**
682 * set the file
683 */
684 public void setFile(String file) {
685
686 if (file == null) {
687 this.file = null;
688 return;
785 }
786
787 // 03/02/2005 b5097243 Pressing 'ESC' on a file dlg does not dispose the dlg on Xtoolkit
788 @SuppressWarnings("deprecation")
789 public void setVisible(boolean b){
790 if (fileDialog == null) {
791 init(target);
792 }
793
794 if (savedDir != null || userDir != null) {
795 setDirectory(savedDir != null ? savedDir : userDir);
796 }
797
798 if (savedFile != null) {
799 // Actually in Motif implementation lost file value which was saved after prevously showing
800 // Seems we shouldn't restore Motif behaviour in this case
801 setFile(savedFile);
802 }
803
804 super.setVisible(b);
805 XChoicePeer choicePeer = AWTAccessor.getComponentAccessor()
806 .getPeer(pathChoice);
807 if (b == true){
808 // See 6240074 for more information
809 choicePeer.addXChoicePeerListener(this);
810 KeyboardFocusManager.getCurrentKeyboardFocusManager()
811 .addKeyEventDispatcher(this);
812 }else{
813 // See 6240074 for more information
814 choicePeer.removeXChoicePeerListener();
815 KeyboardFocusManager.getCurrentKeyboardFocusManager()
816 .removeKeyEventDispatcher(this);
817 }
818
819 selectionField.requestFocusInWindow();
820 }
821
822 /*
823 * Adding items to the path choice based on the text string
824 * See 6240074 for more information
825 */
826 public void addItemsToPathChoice(String text){
827 String dirList[] = getDirList(text);
828 for (int i = 0; i < dirList.length; i++) pathChoice.addItem(dirList[i]);
829 }
830
831 /*
832 * Refresh the unfurled choice at the time of the opening choice according to the text of the path field
833 * See 6240074 for more information
834 */
835 public void unfurledChoiceOpening(ListHelper choiceHelper){
836
|