/src/server/gwtgui/src/keymind/keywatch/gui/client/desktop/TabItemWidget.java

http://keywatch.googlecode.com/ · Java · 179 lines · 107 code · 18 blank · 54 comment · 16 complexity · dc4c5d50d308cf475c45c9176f2a66a6 MD5 · raw file

  1. /**
  2. * -----------------------------------------------------------------------------------------------
  3. * File: TabItemWidget.java
  4. *
  5. * Copyright (c) 2007 by Keymind Computing as.
  6. * All rights reserved.
  7. *
  8. * This file is subject to the terms and conditions of the Apache Licence 2.0.
  9. * See the file LICENCE in the main directory of the Keywatch distribution for more details.
  10. *
  11. * Revision History:
  12. * $URL: http://keywatch.googlecode.com/svn/trunk/src/server/gwtgui/src/keymind/keywatch/gui/client/desktop/TabItemWidget.java $
  13. * $Date: 2009-09-17 11:14:51 +0200 (Thu, 17 Sep 2009) $, $Rev: $
  14. * -----------------------------------------------------------------------------------------------
  15. */
  16. package keymind.keywatch.gui.client.desktop;
  17. import com.google.gwt.event.dom.client.ClickEvent;
  18. import com.google.gwt.event.dom.client.ClickHandler;
  19. import com.google.gwt.event.logical.shared.CloseHandler;
  20. import com.google.gwt.event.logical.shared.CloseEvent;
  21. import com.google.gwt.user.client.Window;
  22. import com.google.gwt.user.client.ui.*;
  23. import keymind.keywatch.gui.client.util.Constants;
  24. /**
  25. * Displays a label and close button. Each desktop tab contains an instance.
  26. */
  27. public class TabItemWidget extends Composite implements ClickHandler
  28. {
  29. private DesktopPanel desktopPanel;
  30. private Label label;
  31. private Image close;
  32. private InputBox inputBox;
  33. static final String PROMPT_TITLE_RENAME = "Rename";
  34. static final String PROMPT_RENAME = "Please specify the new desktop name.";
  35. static final String PROMPT_REMOVE = "Are you sure you want to remove the current desktop tab from you profile?";
  36. static final String TABNAME_SETTINGS = "Settings";
  37. static final String RENAME_NOT_UNIQUE = "Could not rename because another desktop with the same name exists";
  38. /**
  39. * C'tor
  40. * @param text Text to display
  41. * @param desktopPanel Owning desktop panel.
  42. */
  43. public TabItemWidget(String text, DesktopPanel desktopPanel)
  44. {
  45. this.desktopPanel = desktopPanel;
  46. FlexTable table = new FlexTable();
  47. label = new Label(text);
  48. label.setWidth(Constants.MAX_PERCENT);
  49. label.addClickHandler(this);
  50. Label space = new Label();
  51. space.setWidth("5px");
  52. close = GUICtrl.getCoreImageBundle().view_close_black().createImage();
  53. close.addClickHandler(this);
  54. // Initialize, the close image is not visible. The desktop framework will change
  55. // visibility as needed.
  56. close.setVisible(false);
  57. table.setWidget(0,0, label);
  58. table.setWidget(0,1, space);
  59. table.setWidget(0,2, close);
  60. initWidget(table);
  61. }
  62. /**
  63. * Show or hide close button
  64. *
  65. * @param visible If true, show close button, else hide it
  66. */
  67. public void setCloseVisible(boolean visible)
  68. {
  69. close.setVisible(visible);
  70. }
  71. /**
  72. * Called when the Close button is pressed. We call back to the owning DesktopPanel
  73. * to do the actual closing.
  74. *
  75. * @param evt Widget that was clicked
  76. */
  77. public void onClick(ClickEvent evt)
  78. {
  79. if(evt.getSource() == close)
  80. {
  81. closeDesktop();
  82. }
  83. else
  84. {
  85. if(GUICtrl.getAppDesktop().getCurrentDesktop() == desktopPanel &&
  86. !desktopPanel.isSettingsDesktop())
  87. {
  88. renameDesktop();
  89. }
  90. }
  91. }
  92. /**
  93. * Rename the desktop
  94. */
  95. private void renameDesktop()
  96. {
  97. // Ask for new name
  98. inputBox = new InputBox(PROMPT_TITLE_RENAME, PROMPT_RENAME);
  99. inputBox.setInputText(desktopPanel.getCategory().getName());
  100. inputBox.addCloseHandler(new CloseHandler<PopupPanel>()
  101. {
  102. public void onClose(CloseEvent<PopupPanel> event)
  103. {
  104. popupClosed(event.getTarget(), event.isAutoClosed());
  105. }
  106. });
  107. // Center and show dialog
  108. GUICtrl.showDialog(inputBox);
  109. }
  110. /**
  111. * A popup window was closed
  112. *
  113. * @param popupPanel The window that was closed
  114. * @param autoClosed True if the popup was closed by clicking outside the control
  115. */
  116. public void popupClosed(PopupPanel popupPanel, boolean autoClosed)
  117. {
  118. if(popupPanel == inputBox)
  119. {
  120. if (!autoClosed && inputBox.isOkClicked())
  121. {
  122. String name = inputBox.getInputText();
  123. if (name != null && name.length() > 0 && !name.equals(TABNAME_SETTINGS))
  124. {
  125. // The name must be unique
  126. if (GUICtrl.getProfileManager().getCategoryByName(name) == null)
  127. {
  128. desktopPanel.getCategory().setName(name);
  129. //GUICtrl.getAppDesktop().renameActiveDesktop(name);
  130. label.setText(name);
  131. // Save immediately
  132. GUICtrl.getProfileManager().setDirty(true);
  133. GUICtrl.getProfileManager().updateProfile(false);
  134. }
  135. else
  136. {
  137. Window.alert(RENAME_NOT_UNIQUE);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Close the desktop
  145. */
  146. private void closeDesktop()
  147. {
  148. if(!desktopPanel.isSettingsDesktop())
  149. {
  150. if (Window.confirm(PROMPT_REMOVE))
  151. {
  152. // Remove the desktop
  153. GUICtrl.getAppDesktop().removeDesktop(desktopPanel);
  154. }
  155. }
  156. else
  157. {
  158. GUICtrl.getAppDesktop().hideSettingsDesktop();
  159. }
  160. }
  161. }