Skip to main content
summaryrefslogtreecommitdiffstats
blob: 424c2a7b2537bf0a7c56b51bfdeaa09da4b8f6c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.widgets;

import java.util.HashSet;
import java.util.Set;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.util.IOseeDictionary;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * @author Donald G. Dunne
 */
public class SkynetSpellModifyDictionary implements XTextSpellModifyDictionary, IOseeDictionary {
   private static Set<String> dictionary;
   private final boolean debug = false;

   @Override
   public boolean addToGlobalDictionary(String word) {
      try {
         return updateArtifact("Global", word, OseeSystemArtifacts.getGlobalPreferenceArtifact());
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
      }
      return false;
   }

   @Override
   public boolean addToLocalDictionary(String word) {
      try {
         return updateArtifact("Local", word, UserManager.getUser());
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
         return false;
      }
   }

   private boolean updateArtifact(String type, String word, Artifact art) {
      if (MessageDialog.openConfirm(Displays.getActiveShell(), "Add to " + type + " Dictionary",
         "Add \"" + word + "\" to " + type + " Dictionary")) {
         try {
            Set<String> words = new HashSet<String>();
            for (String str : art.getSoleAttributeValue(CoreAttributeTypes.Dictionary, "").split(";")) {
               words.add(str);
            }
            words.add(word);
            art.setSoleAttributeValue(CoreAttributeTypes.Dictionary, Collections.toString(";", words));
            art.persist(getClass().getSimpleName());
            loadDictionary(true);
            return true;
         } catch (Exception ex) {
            OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
         }
      }
      return false;
   }

   @Override
   public boolean isWord(String word) {
      loadDictionary(false);
      boolean contains = dictionary.contains(word);
      if (debug) {
         System.out.println("Checking OSEESpell => \"" + word + "\" " + contains);
      }
      if (debug) {
         System.out.println("OSEESpellDict => " + Collections.toString(",", dictionary));
      }
      return contains;
   }

   public void loadDictionary(boolean force) {
      if (!force && dictionary != null) {
         return;
      }
      try {
         dictionary = new HashSet<String>();
         User user = UserManager.getUser();
         if (user != null) {
            String value = user.getSoleAttributeValue(CoreAttributeTypes.Dictionary, "");
            if (value != null) {
               String[] entries = value.split(";");
               for (String str : entries) {
                  if (debug) {
                     System.out.println("Adding Local => \"" + str + "\"");
                  }
                  if (Strings.isValid(str)) {
                     dictionary.add(str);
                  }
               }
               for (String str : OseeSystemArtifacts.getGlobalPreferenceArtifact().getSoleAttributeValue(
                  CoreAttributeTypes.Dictionary, "").split(";")) {
                  if (debug) {
                     System.out.println("Adding Global => \"" + str + "\"");
                  }
                  if (Strings.isValid(str)) {
                     dictionary.add(str);
                  }
               }
            }
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
      }
   }
}

Back to the top