Skip to content

Commit

Permalink
Added Quick-Commands for GenModel-Generation and Project-Creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sgraband authored and leodennis committed Jan 30, 2020
1 parent 9e5d239 commit fa2d6d2
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
143 changes: 143 additions & 0 deletions client/theia-ecore/src/browser/CreateProjectCommandContribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { FrontendApplication, OpenerService, QuickOpenOptions, QuickOpenService } from "@theia/core/lib/browser";
import { Command, CommandContribution, CommandRegistry } from "@theia/core/lib/common/command";
import { MessageService } from "@theia/core/lib/common/message-service";
import { ProgressService } from "@theia/core/lib/common/progress-service";
import { SelectionService } from "@theia/core/lib/common/selection-service";
import URI from "@theia/core/lib/common/uri";
import { UriAwareCommandHandler, UriCommandHandler } from "@theia/core/lib/common/uri-command-handler";
import { EDITOR_CONTEXT_MENU } from "@theia/editor/lib/browser";
import { FileDialogService } from "@theia/filesystem/lib/browser";
import { FileStat, FileSystem } from "@theia/filesystem/lib/common/filesystem";
import { NAVIGATOR_CONTEXT_MENU } from "@theia/navigator/lib/browser/navigator-contribution";
import { WorkspaceService } from "@theia/workspace/lib/browser";
import { inject, injectable } from "inversify";

import { FileGenServer } from "../common/generate-protocol";



export const EXAMPLE_NAVIGATOR = [...NAVIGATOR_CONTEXT_MENU, 'example'];
export const EXAMPLE_EDITOR = [...EDITOR_CONTEXT_MENU, 'example'];



export const CREATE_NEW_PROJECT: Command = {
id: 'file.createNewProject',
category: 'File',
label: 'Create new Project',
};

@injectable()
export class CreateProjectCommandContribution implements CommandContribution {

@inject(FileSystem) protected readonly fileSystem: FileSystem;
@inject(SelectionService) protected readonly selectionService: SelectionService;
@inject(OpenerService) protected readonly openerService: OpenerService;
@inject(FrontendApplication) protected readonly app: FrontendApplication;
@inject(MessageService) protected readonly messageService: MessageService;
@inject(FileDialogService) protected readonly fileDialogService: FileDialogService;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(ProgressService) protected readonly progressService: ProgressService;
@inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService;
@inject(FileGenServer) private readonly fileGenServer: FileGenServer;

registerCommands(registry: CommandRegistry): void {
registry.registerCommand(CREATE_NEW_PROJECT, this.newWorkspaceRootUriAwareCommandHandler({
execute: uri => this.getDirectory(uri).then(parent => {
if (parent) {
const parentUri = new URI(parent.uri);
// @Leo hier wird die Methode zum generieren aufgerufen.
// Die Variable uri ist das aktuell markierte Element
// und parentUri ist dann dementsprechend der Ordner darüber(wrsl der Workspace)
this.fileGenServer.createNewProject().then(() => {
// @Leo hier kannst du die files nachdem sie generiert wurden öffnen
});
}
})
}));
}

protected withProgress<T>(task: () => Promise<T>): Promise<T> {
return this.progressService.withProgress('', 'scm', task);
}

protected newUriAwareCommandHandler(handler: UriCommandHandler<URI>): UriAwareCommandHandler<URI> {
return new UriAwareCommandHandler(this.selectionService, handler);
}

protected newMultiUriAwareCommandHandler(handler: UriCommandHandler<URI[]>): UriAwareCommandHandler<URI[]> {
return new UriAwareCommandHandler(this.selectionService, handler, { multi: true });
}

protected newWorkspaceRootUriAwareCommandHandler(handler: UriCommandHandler<URI>): WorkspaceRootUriAwareCommandHandler {
return new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, handler);
}

protected async getDirectory(candidate: URI): Promise<FileStat | undefined> {
const stat = await this.fileSystem.getFileStat(candidate.toString());
if (stat && stat.isDirectory) {
return stat;
}
return this.getParent(candidate);
}

protected getParent(candidate: URI): Promise<FileStat | undefined> {
return this.fileSystem.getFileStat(candidate.parent.toString());
}

private getOptions(placeholder: string, fuzzyMatchLabel: boolean = true, onClose: (canceled: boolean) => void = () => { }): QuickOpenOptions {
return QuickOpenOptions.resolve({
placeholder,
fuzzyMatchLabel,
fuzzySort: false,
onClose
});
}
}

export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<URI> {

constructor(
protected readonly workspaceService: WorkspaceService,
protected readonly selectionService: SelectionService,
protected readonly handler: UriCommandHandler<URI>
) {
super(selectionService, handler);
}

public isEnabled(...args: any[]): boolean {
return super.isEnabled(...args) && !!this.workspaceService.tryGetRoots().length;
}

public isVisible(...args: any[]): boolean {
return super.isVisible(...args) && !!this.workspaceService.tryGetRoots().length;
}

protected getUri(...args: any[]): URI | undefined {
const uri = super.getUri(...args);
// If the URI is available, return it immediately.
if (uri) {
return uri;
}
// Return the first root if available.
if (!!this.workspaceService.tryGetRoots().length) {
return new URI(this.workspaceService.tryGetRoots()[0].uri);
}
return undefined;
}
}
143 changes: 143 additions & 0 deletions client/theia-ecore/src/browser/GenerateGenModelCommandContribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { FrontendApplication, OpenerService, QuickOpenOptions, QuickOpenService } from "@theia/core/lib/browser";
import { Command, CommandContribution, CommandRegistry } from "@theia/core/lib/common/command";
import { MessageService } from "@theia/core/lib/common/message-service";
import { ProgressService } from "@theia/core/lib/common/progress-service";
import { SelectionService } from "@theia/core/lib/common/selection-service";
import URI from "@theia/core/lib/common/uri";
import { UriAwareCommandHandler, UriCommandHandler } from "@theia/core/lib/common/uri-command-handler";
import { EDITOR_CONTEXT_MENU } from "@theia/editor/lib/browser";
import { FileDialogService } from "@theia/filesystem/lib/browser";
import { FileStat, FileSystem } from "@theia/filesystem/lib/common/filesystem";
import { NAVIGATOR_CONTEXT_MENU } from "@theia/navigator/lib/browser/navigator-contribution";
import { WorkspaceService } from "@theia/workspace/lib/browser";
import { inject, injectable } from "inversify";

import { FileGenServer } from "../common/generate-protocol";



export const EXAMPLE_NAVIGATOR = [...NAVIGATOR_CONTEXT_MENU, 'example'];
export const EXAMPLE_EDITOR = [...EDITOR_CONTEXT_MENU, 'example'];



export const GENERATE_GENMODEL: Command = {
id: 'file.generateGenModel',
category: 'File',
label: 'Generate GenModel',
};

@injectable()
export class GenerateGenModelCommandContribution implements CommandContribution {

@inject(FileSystem) protected readonly fileSystem: FileSystem;
@inject(SelectionService) protected readonly selectionService: SelectionService;
@inject(OpenerService) protected readonly openerService: OpenerService;
@inject(FrontendApplication) protected readonly app: FrontendApplication;
@inject(MessageService) protected readonly messageService: MessageService;
@inject(FileDialogService) protected readonly fileDialogService: FileDialogService;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(ProgressService) protected readonly progressService: ProgressService;
@inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService;
@inject(FileGenServer) private readonly fileGenServer: FileGenServer;

registerCommands(registry: CommandRegistry): void {
registry.registerCommand(GENERATE_GENMODEL, this.newWorkspaceRootUriAwareCommandHandler({
execute: uri => this.getDirectory(uri).then(parent => {
if (parent) {
const parentUri = new URI(parent.uri);
// @Leo hier wird die Methode zum generieren aufgerufen.
// Die Variable uri ist das aktuell markierte Element
// und parentUri ist dann dementsprechend der Ordner darüber(wrsl der Workspace)
this.fileGenServer.generateGenModel().then(() => {
// @Leo hier kannst du die files nachdem sie generiert wurden öffnen
});
}
})
}));
}

protected withProgress<T>(task: () => Promise<T>): Promise<T> {
return this.progressService.withProgress('', 'scm', task);
}

protected newUriAwareCommandHandler(handler: UriCommandHandler<URI>): UriAwareCommandHandler<URI> {
return new UriAwareCommandHandler(this.selectionService, handler);
}

protected newMultiUriAwareCommandHandler(handler: UriCommandHandler<URI[]>): UriAwareCommandHandler<URI[]> {
return new UriAwareCommandHandler(this.selectionService, handler, { multi: true });
}

protected newWorkspaceRootUriAwareCommandHandler(handler: UriCommandHandler<URI>): WorkspaceRootUriAwareCommandHandler {
return new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, handler);
}

protected async getDirectory(candidate: URI): Promise<FileStat | undefined> {
const stat = await this.fileSystem.getFileStat(candidate.toString());
if (stat && stat.isDirectory) {
return stat;
}
return this.getParent(candidate);
}

protected getParent(candidate: URI): Promise<FileStat | undefined> {
return this.fileSystem.getFileStat(candidate.parent.toString());
}

private getOptions(placeholder: string, fuzzyMatchLabel: boolean = true, onClose: (canceled: boolean) => void = () => { }): QuickOpenOptions {
return QuickOpenOptions.resolve({
placeholder,
fuzzyMatchLabel,
fuzzySort: false,
onClose
});
}
}

export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<URI> {

constructor(
protected readonly workspaceService: WorkspaceService,
protected readonly selectionService: SelectionService,
protected readonly handler: UriCommandHandler<URI>
) {
super(selectionService, handler);
}

public isEnabled(...args: any[]): boolean {
return super.isEnabled(...args) && !!this.workspaceService.tryGetRoots().length;
}

public isVisible(...args: any[]): boolean {
return super.isVisible(...args) && !!this.workspaceService.tryGetRoots().length;
}

protected getUri(...args: any[]): URI | undefined {
const uri = super.getUri(...args);
// If the URI is available, return it immediately.
if (uri) {
return uri;
}
// Return the first root if available.
if (!!this.workspaceService.tryGetRoots().length) {
return new URI(this.workspaceService.tryGetRoots()[0].uri);
}
return undefined;
}
}
4 changes: 4 additions & 0 deletions client/theia-ecore/src/browser/frontend-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import { ContainerModule, interfaces } from "inversify";
import { DiagramConfiguration, DiagramManager, DiagramManagerProvider } from "sprotty-theia/lib";

import { FILEGEN_SERVICE_PATH, FileGenServer } from "../common/generate-protocol";
import { CreateProjectCommandContribution } from "./CreateProjectCommandContribution";
import { EcoreDiagramConfiguration } from "./diagram/ecore-diagram-configuration";
import { EcoreDiagramManager } from "./diagram/ecore-diagram-manager.";
import { EcoreGLSPDiagramClient } from "./diagram/ecore-glsp-diagram-client";
import { EcoreGLSPClientContribution } from "./ecore-glsp--contribution";
import { EcoreCommandContribution } from "./EcoreCommandContribution";
import { GenerateCodeCommandContribution } from "./GenerateCodeCommandContribution";
import { GenerateGenModelCommandContribution } from "./GenerateGenModelCommandContribution";


export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
Expand All @@ -56,4 +58,6 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
return connection.createProxy<FileGenServer>(FILEGEN_SERVICE_PATH);
}).inSingletonScope();
bind(CommandContribution).to(GenerateCodeCommandContribution);
bind(CommandContribution).to(GenerateGenModelCommandContribution);
bind(CommandContribution).to(CreateProjectCommandContribution);
});
2 changes: 2 additions & 0 deletions client/theia-ecore/src/common/generate-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export interface FileGenServer extends JsonRpcServer<undefined> {
generateEcore(name: string, prefix: string, uri: string, path: string): Promise<string>
// @Leo hier ist das Interface. Parameter müssen hier wahrscheinlich noch angepasst werden.
generateCode(): Promise<String>
generateGenModel(): Promise<String>
createNewProject(): Promise<String>
}
8 changes: 8 additions & 0 deletions client/theia-ecore/src/node/ecore-file-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export class EcoreFileGenServer implements FileGenServer, BackendApplicationCont
throw new Error("Method not implemented.");
}

generateGenModel(): Promise<String> {
throw new Error("Method not implemented.");
}

createNewProject(): Promise<String> {
throw new Error("Method not implemented.");
}

onStop(app?: Application): void {
this.dispose();
}
Expand Down

0 comments on commit fa2d6d2

Please sign in to comment.