• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
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 
17 package com.google.googlejavaformat.intellij;
18 
19 import com.intellij.notification.Notification;
20 import com.intellij.notification.NotificationGroup;
21 import com.intellij.notification.NotificationGroupManager;
22 import com.intellij.notification.NotificationType;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.startup.StartupActivity;
25 import org.jetbrains.annotations.NotNull;
26 
27 final class InitialConfigurationStartupActivity implements StartupActivity.Background {
28 
29   private static final String NOTIFICATION_TITLE = "Enable google-java-format";
30   private static final NotificationGroup NOTIFICATION_GROUP =
31       NotificationGroupManager.getInstance().getNotificationGroup(NOTIFICATION_TITLE);
32 
33   @Override
runActivity(@otNull Project project)34   public void runActivity(@NotNull Project project) {
35     GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
36 
37     if (settings.isUninitialized()) {
38       settings.setEnabled(false);
39       displayNewUserNotification(project, settings);
40     } else if (settings.isEnabled()) {
41       JreConfigurationChecker.checkJreConfiguration(project);
42     }
43   }
44 
displayNewUserNotification(Project project, GoogleJavaFormatSettings settings)45   private void displayNewUserNotification(Project project, GoogleJavaFormatSettings settings) {
46     Notification notification =
47         new Notification(
48             NOTIFICATION_GROUP.getDisplayId(),
49             NOTIFICATION_TITLE,
50             "The google-java-format plugin is disabled by default. "
51                 + "<a href=\"enable\">Enable for this project</a>.",
52             NotificationType.INFORMATION);
53     notification.setListener(
54         (n, e) -> {
55           settings.setEnabled(true);
56           n.expire();
57         });
58     notification.notify(project);
59   }
60 }
61