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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/**
Modeler.m
Author: Matt Rice <ratmice@gmail.com>
Date: Apr 2005
This file is part of DBModeler.
<license>
DBModeler is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
DBModeler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DBModeler; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
</license>
**/
#include "AdaptorsPanel.h"
#include "ConsistencyChecker.h"
#include "ConsistencyResults.h"
#include "MainModelEditor.h"
#include "Modeler.h"
#include "ModelerEntityEditor.h"
#include "SQLGenerator.h"
#include "Preferences.h"
#include "CodeGenerator.h"
#include <EOModeler/EOModelerApp.h>
#include <EOModeler/EOModelerEditor.h>
#include <EOModeler/EOModelerDocument.h>
#include <EOModeler/EOMInspectorController.h>
#include <EOAccess/EOModel.h>
#include <EOAccess/EOModelGroup.h>
#include <EOAccess/EOAdaptorChannel.h>
#include <EOAccess/EOAdaptorContext.h>
#include <EOAccess/EOAdaptor.h>
#include <EOControl/EOObserver.h>
#ifdef NeXT_GUI_LIBRARY
#include <AppKit/AppKit.h>
#else
#include <AppKit/NSOpenPanel.h>
#endif
#ifdef NeXT_Foundation_LIBRARY
#include <Foundation/Foundation.h>
#else
#include <Foundation/NSObject.h>
#include <Foundation/NSFileManager.h>
#endif
#include <GNUstepBase/GNUstep.h>
@interface DebugObserver : NSObject <EOObserving>
{
}
@end
@implementation DebugObserver
- (void) objectWillChange:(id)subj
{
NSLog(@"%@ %@ %@", NSStringFromSelector(_cmd), [subj class], subj);
}
@end
@interface NSMenu (im_lazy)
- (NSMenuItem *)addItemWithTitle: (NSString *)s;
- (NSMenuItem *)addItemWithTitle: (NSString *)s action: (SEL)sel;
@end
@implementation NSMenu (im_lazy)
- (NSMenuItem *)addItemWithTitle: (NSString *)s
{
return [self addItemWithTitle: s action: NULL keyEquivalent: @""];
}
- (NSMenuItem *)addItemWithTitle: (NSString *)s action: (SEL)sel
{
return [self addItemWithTitle: s action: sel keyEquivalent: @""];
}
@end
@implementation Modeler
-(void)bundleDidLoad:(NSNotification *)not
{
/* a place to put breakpoints? */
}
- (void) applicationWillFinishLaunching:(NSNotification *)not
{
int i,c;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *bundlesToLoad = RETAIN([defaults arrayForKey: @"BundlesToLoad"]);
NSFileManager *fm = [NSFileManager defaultManager];
// [EOObserverCenter addOmniscientObserver:[DebugObserver new]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bundleDidLoad:)
name:NSBundleDidLoadNotification
object:nil];
for (i=0, c = [bundlesToLoad count]; i < c; i++)
{
BOOL isDir;
NSString *path = [[bundlesToLoad objectAtIndex:i] stringByExpandingTildeInPath];
[fm fileExistsAtPath:path isDirectory:&isDir];
if (isDir)
{
NSLog(@"loading bundle: %@",path);
[[[NSBundle bundleWithPath: path] principalClass] class]; //call class so +initialize gets called
}
}
RELEASE(bundlesToLoad);
/* useful method for setting breakpoints after an adaptor is loaded */
[ConsistencyChecker class];
/* make this a default? */
[EOModelerDocument setDefaultEditorClass: NSClassFromString(@"MainModelEditor")];
}
- (void) showEditor:(id)sender
{
EOModelerCompoundEditor *ed = (id)[EOMApp currentEditor];
if ([[sender title] isEqual:_(@"Diagram Editor")])
{
[ed activateEmbeddedEditor: [ed embedibleEditorOfClass:NSClassFromString(@"DiagramEditor")]];
}
else if ([[sender title] isEqual:_(@"Table Editor")])
{
[ed activateEmbeddedEditor: [ed embedibleEditorOfClass:NSClassFromString(@"ModelerEntityEditor")]];
}
}
- (void) _newDocumentWithModel:(EOModel *)newModel
{
EOModelerDocument *newModelerDoc;
EOModelerCompoundEditor *editor;
newModelerDoc = [[EOModelerDocument alloc] initWithModel: newModel];
editor = (EOModelerCompoundEditor*)[newModelerDoc addDefaultEditor];
[EOMApp setCurrentEditor: editor];
[EOMApp addDocument: newModelerDoc];
RELEASE(newModelerDoc);
[newModelerDoc activate];
}
- (void)new:(id)sender
{
EOModel *newModel = [[EOModel alloc] init];
NSString *modelName;
NSArray *docs = [EOMApp documents];
unsigned docNumber, c, i;
docNumber = [[EOMApp documents] count];
c = [docs count];
docNumber = c;
/* look for the largest NNNN in models named "Model_NNNN"
* or the total number of models whichever is greater.
*/
for (i = 0; i < c; i++)
{
NSString *name = [(EOModel*)[[docs objectAtIndex:i] model] name];
if ([name hasPrefix:@"Model_"])
{
NSRange range;
unsigned tmp;
name = [name substringFromIndex:6];
range = [name rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
if (!(range.location == NSNotFound) && !(range.length == 0))
continue;
range = [name rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
if (!(range.location == NSNotFound) && !(range.length == 0))
{
tmp = [name intValue];
docNumber = (docNumber < ++tmp) ? tmp : docNumber;
}
}
}
modelName = [NSString stringWithFormat:@"Model_%u",docNumber];
[newModel setName:modelName];
[self _newDocumentWithModel:newModel];
RELEASE(newModel);
}
- (void) newFromDatabase:(id)sender
{
NSString *adaptorName;
AdaptorsPanel *adaptorsPanel = [[AdaptorsPanel alloc] init];
adaptorName = [adaptorsPanel runAdaptorsPanel];
RELEASE(adaptorsPanel);
if (adaptorName)
{
EOAdaptor *adaptor;
EOAdaptorChannel *channel;
EOAdaptorContext *ctxt;
EOModel *newModel;
NSDictionary *connDict;
adaptor = [EOAdaptor adaptorWithName:adaptorName];
connDict = [adaptor runLoginPanel];
if (connDict)
{
[adaptor setConnectionDictionary:connDict];
ctxt = [adaptor createAdaptorContext];
channel = [ctxt createAdaptorChannel];
[channel openChannel];
newModel = [channel describeModelWithTableNames:[channel describeTableNames]];
[newModel setConnectionDictionary:[adaptor connectionDictionary]];
[newModel setName: [[adaptor connectionDictionary] objectForKey:@"databaseName"]];
[channel closeChannel];
[self _newDocumentWithModel:newModel];
}
}
}
- (BOOL) validateMenuItem:(NSMenuItem *)menuItem
{
if (sel_isEqual([menuItem action], @selector(createTemplate:))) {
return YES;
}
if ([[menuItem title] isEqualToString:@"Set Adaptor Info"])
{
return ([EOMApp activeDocument] != nil);
}
return YES;
}
- (void) setAdaptor:(id)sender
{
NSString *adaptorName;
EOAdaptor *adaptor;
AdaptorsPanel *adaptorsPanel = [[AdaptorsPanel alloc] init];
adaptorName = [adaptorsPanel runAdaptorsPanel];
RELEASE(adaptorsPanel);
if (!adaptorName)
return;
[[[EOMApp activeDocument] model] setAdaptorName: adaptorName];
adaptor = [EOAdaptor adaptorWithName: adaptorName];
[[[EOMApp activeDocument] model] setConnectionDictionary:[adaptor runLoginPanel]];
}
- (void) showInspector:(id)sender
{
[EOMInspectorController showInspector];
}
- (BOOL) application:(NSApplication *)theApp openFile:(NSString *)filename
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *pathExt = [[filename pathExtension] lowercaseString];
BOOL flag;
#if 0
NSLog(@"%@ %@ %i %i %i %i %i", NSStringFromSelector(_cmd), filename,
[fm isReadableFileAtPath:filename] == YES,
[pathExt isEqual:@"eomodel"],
[pathExt isEqual:@"eomodeld"],
[fm fileExistsAtPath:filename isDirectory:&flag],
flag);
#endif
if (([fm isReadableFileAtPath:filename] == YES
&& [pathExt isEqual:@"eomodel"])
|| ([pathExt isEqual:@"eomodeld"]
&& [fm fileExistsAtPath:filename isDirectory:&flag] && flag))
{
EOModel *model;
NS_DURING
model = [[EOModel alloc] initWithContentsOfFile:filename];
NS_HANDLER
return NO;
NS_ENDHANDLER
[self _newDocumentWithModel:model];
RELEASE(model);
return YES;
}
return NO;
}
- (void) open:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSFileManager *fm = [NSFileManager defaultManager];
if ([panel runModalForTypes:[NSArray arrayWithObjects:@"eomodeld",@"eomodel",nil]] == NSOKButton)
{
NSArray *modelPaths = [panel filenames];
int i,c;
for (i = 0, c = [modelPaths count]; i < c; i++)
{
NSString *modelPath = [modelPaths objectAtIndex:i];
NSString *pathExt = [[modelPath pathExtension] lowercaseString];
if ([fm isReadableFileAtPath:modelPath] == YES
&& ([pathExt isEqual:@"eomodeld"]
|| [pathExt isEqual:@"eomodel"]))
{
EOModel *model;
NS_DURING
model = [[EOModel alloc] initWithContentsOfFile:modelPath];
NS_HANDLER
return;
NS_ENDHANDLER
[self _newDocumentWithModel:model];
RELEASE(model);
}
}
}
}
- (void) generateSQL:(id)sender
{
[[SQLGenerator sharedGenerator] openSQLGenerator:self];
}
- (void) openPrefs:(id)sender
{
[[DBModelerPrefs sharedPreferences] showPreferences:self];
}
- (void)createTemplate:(id)sender
{
CodeGenerator * codeGen = [[CodeGenerator new] autorelease];
[codeGen generate];
}
@end
|