/plugins/android-designer/src/com/intellij/android/designer/actions/ProfileAction.java

https://bitbucket.org/nbargnesi/idea · Java · 173 lines · 130 code · 25 blank · 18 comment · 21 complexity · bc43c4fda71d1beca8a8b92c3593cf28 MD5 · raw file

  1. /*
  2. * Copyright 2000-2012 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.android.designer.actions;
  17. import com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel;
  18. import com.intellij.android.designer.profile.Profile;
  19. import com.intellij.android.designer.profile.ProfileDialog;
  20. import com.intellij.android.designer.profile.ProfileList;
  21. import com.intellij.android.designer.profile.ProfileManager;
  22. import com.intellij.designer.actions.AbstractComboBoxAction;
  23. import com.intellij.openapi.actionSystem.DefaultActionGroup;
  24. import com.intellij.openapi.actionSystem.Presentation;
  25. import com.intellij.openapi.projectRoots.Sdk;
  26. import com.intellij.openapi.ui.DialogWrapper;
  27. import org.jetbrains.annotations.Nullable;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. /**
  31. * @author Alexander Lobas
  32. */
  33. public class ProfileAction {
  34. private static final Profile EDIT_PROFILE = new Profile();
  35. static {
  36. EDIT_PROFILE.setName("Edit Profiles");
  37. }
  38. private final AndroidDesignerEditorPanel myDesigner;
  39. private final ProfileManager myProfileManager;
  40. private final AbstractComboBoxAction<Profile> myProfileAction;
  41. private final DefaultActionGroup myActionGroup = new DefaultActionGroup();
  42. private final ProfileList myProfileList;
  43. private int myVersion;
  44. public ProfileAction(AndroidDesignerEditorPanel designer, Runnable refreshAction) {
  45. myDesigner = designer;
  46. myProfileList = ProfileList.getInstance(myDesigner.getProject());
  47. myProfileManager = new ProfileManager(myDesigner, refreshAction, new Runnable() {
  48. @Override
  49. public void run() {
  50. myProfileList.addVersion();
  51. myVersion = myProfileList.getVersion();
  52. }
  53. });
  54. myProfileAction = new AbstractComboBoxAction<Profile>() {
  55. @Override
  56. protected boolean addSeparator(DefaultActionGroup actionGroup, Profile item) {
  57. if (item == EDIT_PROFILE) {
  58. actionGroup.addSeparator();
  59. }
  60. return false;
  61. }
  62. @Override
  63. protected void update(Profile item, Presentation presentation, boolean popup) {
  64. presentation.setText(item.getName());
  65. }
  66. @Override
  67. protected boolean selectionChanged(Profile item) {
  68. if (item == EDIT_PROFILE) {
  69. editProfiles();
  70. }
  71. else {
  72. myProfileList.addVersion();
  73. myVersion = myProfileList.getVersion();
  74. updateActions(item);
  75. }
  76. return item != EDIT_PROFILE;
  77. }
  78. };
  79. DefaultActionGroup designerActionGroup = myDesigner.getActionPanel().getActionGroup();
  80. designerActionGroup.add(myProfileAction);
  81. designerActionGroup.add(myActionGroup);
  82. updateActions();
  83. }
  84. public ProfileManager getProfileManager() {
  85. return myProfileManager;
  86. }
  87. private void updateActions() {
  88. myVersion = myProfileList.getVersion();
  89. List<Profile> profiles = new ArrayList<Profile>(myProfileList.getProfiles());
  90. profiles.add(myProfileList.getFullProfile());
  91. profiles.add(EDIT_PROFILE);
  92. Profile profile = myProfileList.getProfile();
  93. myProfileAction.setItems(profiles, profile);
  94. updateActions(profile);
  95. }
  96. private void updateActions(Profile profile) {
  97. myProfileManager.setProfile(profile);
  98. myProfileList.setSelection(profile.getName());
  99. myActionGroup.removeAll();
  100. if (profile.isShowDevice()) {
  101. myActionGroup.add(myProfileManager.getDeviceAction());
  102. }
  103. if (profile.isShowDeviceConfiguration()) {
  104. myActionGroup.add(myProfileManager.getDeviceConfigurationAction());
  105. }
  106. if (profile.isShowTarget()) {
  107. myActionGroup.add(myProfileManager.getTargetAction());
  108. }
  109. if (profile.isShowLocale()) {
  110. myActionGroup.add(myProfileManager.getLocaleAction());
  111. }
  112. if (profile.isShowDockMode()) {
  113. myActionGroup.add(myProfileManager.getDockModeAction());
  114. }
  115. if (profile.isShowNightMode()) {
  116. myActionGroup.add(myProfileManager.getNightModeAction());
  117. }
  118. if (profile.isShowTheme()) {
  119. myActionGroup.add(myProfileManager.getThemeAction());
  120. }
  121. myDesigner.getActionPanel().update();
  122. }
  123. private void editProfiles() {
  124. ProfileDialog dialog = new ProfileDialog(myDesigner, myProfileList.getProfiles());
  125. dialog.show();
  126. if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
  127. myProfileList.setProfiles(dialog.getResult());
  128. myProfileList.addVersion();
  129. updateActions();
  130. }
  131. }
  132. public void externalUpdate() {
  133. Sdk sdk = myProfileList.getModuleSdk(myDesigner.getModule());
  134. if ((sdk != null && !sdk.equals(getCurrentSdk()))) {
  135. myProfileManager.update(sdk);
  136. }
  137. else if (myVersion != myProfileList.getVersion()) {
  138. updateActions();
  139. }
  140. }
  141. @Nullable
  142. public Sdk getCurrentSdk() {
  143. return myProfileManager.getSdk();
  144. }
  145. public int getVersion() {
  146. return myVersion;
  147. }
  148. }